From a58057e30b13e0946008476bee3c8843ed581a76 Mon Sep 17 00:00:00 2001 From: sinakratos Date: Sat, 11 Jun 2022 10:57:46 +0430 Subject: [PATCH 1/4] made the vector boolian type --- main.cpp | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a564979 --- /dev/null +++ b/main.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace cv; + +// global variables +const int pixel = 100; // the size of each block generated +vector> vec; +int H_ratio, V_ratio; +Mat image; + +// coordination struct to use in the callback function +struct initRoi { + + bool init = false; // on off + + // initial coordination based on EVENT_LBUTTONDOWN + int initX; + int initY; + + bool grabType; // it's for when you drag a black block, you don't make any blalck blocks white or the other turn around +} SelectedRoi; + +// the functions +void CallBackFunc(int event, int x, int y, int flags, void* userdata); +void ChangeColor(Mat image, vector>& vec, int x, int y); +void MakeBlocks(Mat image, vector>& vec); + +// main +int main() { + system("clear"); + + // getting the ratio of the output. (it's suggested to be a low number otherwise it will crash) + cout << "Enter the ratio: "; + cin >> H_ratio >> V_ratio; + + // resizing the vector based on the ratio and giving '0' values to the vector + vec.resize(H_ratio, vector(V_ratio, false)); + + // making a new mat and copying it to the global one + Mat img(H_ratio*pixel, V_ratio*pixel, CV_8UC3, Scalar(255, 255, 255)); + image = img; + + MakeBlocks(image, vec); // makes the blocks + imshow("Output", image); // show the output image + setMouseCallback("Output", CallBackFunc); // a callback function + + waitKey(0); +} + +// this function has the algorithm for the rectangles to be made, and makes them +void MakeBlocks(Mat image, vector>& vec) { + int thickness = 2; // thickness of the border of rectangles + + for(int i = 0; i < vec.size(); i++) { + for(int j = 0; j < vec[i].size(); j++) { + Point p1(j*pixel, i*pixel); // upper left corner + Point p2((j + 1)*pixel, (i + 1)*pixel); // bottom right corner + rectangle(image, p1, p2, Scalar(0, 0, 0), thickness, LINE_8); + } + } +} + +// any events happening by the mouse is determined in this function +void CallBackFunc(int event, int x, int y, int flags, void* userdata) { + int X = x / pixel, Y = y / pixel; // by using these we will know what exact block we are on + + // determines if the left button is down + if(event == EVENT_LBUTTONDOWN) { + SelectedRoi.initX = x; // saving the x value to 'initX' + SelectedRoi.initY = y; // saving the y value to 'initY' + SelectedRoi.init = true; // by givin 'init' the value of 'true' we are telling the program that the button is still down + + // by doing this we are making the program not to change the blocks that were previously black back to white + if(vec[X][Y]) { + SelectedRoi.grabType = false; + } + + // by doing this we are making the program not to change the blocks that were previously white back to black + if(!vec[X][Y]) { + SelectedRoi.grabType = true; + } + + // calls the function that changes the color the blocks + ChangeColor(image, vec, x, y); + } + + // determines if the button is not down anymore + if (event == EVENT_LBUTTONUP) { + SelectedRoi.init = false; // button is now down anymore (value == false) + } + + // making two new values to use later on + int changeY = abs(floor((SelectedRoi.initX / pixel)) - (X)); // knowing the block change in the x axis + int changeX = abs(floor((SelectedRoi.initY / pixel)) - (Y)); // knowing the block change in the y axis + + // if the left button is still down And the mouse moves: + if(SelectedRoi.init && event == EVENT_MOUSEMOVE) { + + // if the block that mouse is on, is changed: + if(changeX >= 1 || changeY >= 1) { + + // if the clicked block is not the same color of the current block that mouse is on + if(SelectedRoi.grabType != vec[X][Y]) { + + ChangeColor(image, vec, x, y); + + // change the initial values if the color is changed + SelectedRoi.initX = x; + SelectedRoi.initY = y; + } + } + } + +} + +// this function changes the blocks' colors +void ChangeColor(Mat image, vector>& vec, int x, int y) { + int thickness = 2; // thickness of the border of rectangles + int X = x / pixel, Y = y / pixel; // by using these we will know what exact block we are on + + // if the block is white it changes to black + if(!vec[X][Y]) { + Point p1(X*pixel, Y*pixel); // upper right corner of the rectangle + Point p2((X + 1) * pixel, (Y + 1) * pixel); // bottom right of the rectangle + rectangle(image, p1, p2, Scalar(0, 0, 0), FILLED); // making the rectangle + vec[X][Y] = true; // reseting the value of the block, because is has been changed + } + + // if the block is black it changes to white + else if(vec[X][Y]) { + Point p1(X * pixel, Y * pixel); // upper right corner of the rectangle + Point p2((X + 1) * pixel, (Y + 1) * pixel); // bottom right of the rectangle + rectangle(image, p1, p2, Scalar(255, 255, 255), FILLED); // making the base white rectangle + rectangle(image, p1, p2, Scalar(0, 0, 0), thickness, LINE_8); // making the outer black rectangle + vec[X][Y] = false; // reseting the value of the block, because is has been changed + } + imshow("Output", image); +} \ No newline at end of file From 3b909200963f82d28f851a16ce9ee47cdfa64552 Mon Sep 17 00:00:00 2001 From: sinakratos Date: Sat, 11 Jun 2022 11:00:17 +0430 Subject: [PATCH 2/4] made start and end point block --- main.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/main.cpp b/main.cpp index a564979..12385f5 100644 --- a/main.cpp +++ b/main.cpp @@ -30,6 +30,7 @@ struct initRoi { void CallBackFunc(int event, int x, int y, int flags, void* userdata); void ChangeColor(Mat image, vector>& vec, int x, int y); void MakeBlocks(Mat image, vector>& vec); +void startEndPoint(); // main int main() { @@ -47,6 +48,7 @@ int main() { image = img; MakeBlocks(image, vec); // makes the blocks + startEndPoint(); imshow("Output", image); // show the output image setMouseCallback("Output", CallBackFunc); // a callback function @@ -141,4 +143,16 @@ void ChangeColor(Mat image, vector>& vec, int x, int y) { vec[X][Y] = false; // reseting the value of the block, because is has been changed } imshow("Output", image); +} + +void startEndPoint() { + // make start point block + rectangle(image, Point(0, 0), Point(pixel, pixel), Scalar(0, 0, 200), FILLED); + rectangle(image, Point(0, 0), Point(pixel, pixel), Scalar(0, 0, 0), 2, LINE_8); + + // make end point block + rectangle(image, Point(pixel * (H_ratio - 1), pixel * (V_ratio - 1)), Point(pixel * H_ratio, pixel * V_ratio), + Scalar(0, 200, 0), FILLED); + rectangle(image, Point(pixel * (H_ratio - 1), pixel * (V_ratio - 1)), Point(pixel * H_ratio, pixel * V_ratio), + Scalar(0, 0, 0), 2, LINE_8); } \ No newline at end of file From 1d6ac8a6b9ba555aa97b479ec25080deb2b931ea Mon Sep 17 00:00:00 2001 From: sinakratos Date: Sat, 11 Jun 2022 11:14:40 +0430 Subject: [PATCH 3/4] fixed start and end point colors geting changed --- main.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 12385f5..e3d6ffc 100644 --- a/main.cpp +++ b/main.cpp @@ -71,9 +71,8 @@ void MakeBlocks(Mat image, vector>& vec) { // any events happening by the mouse is determined in this function void CallBackFunc(int event, int x, int y, int flags, void* userdata) { int X = x / pixel, Y = y / pixel; // by using these we will know what exact block we are on - // determines if the left button is down - if(event == EVENT_LBUTTONDOWN) { + if(event == EVENT_LBUTTONDOWN && (X != 0 || Y != 0) && (X != H_ratio - 1 || Y != V_ratio - 1)) { SelectedRoi.initX = x; // saving the x value to 'initX' SelectedRoi.initY = y; // saving the y value to 'initY' SelectedRoi.init = true; // by givin 'init' the value of 'true' we are telling the program that the button is still down @@ -108,7 +107,7 @@ void CallBackFunc(int event, int x, int y, int flags, void* userdata) { if(changeX >= 1 || changeY >= 1) { // if the clicked block is not the same color of the current block that mouse is on - if(SelectedRoi.grabType != vec[X][Y]) { + if(SelectedRoi.grabType != vec[X][Y] && (X != 0 || Y != 0) && (X != H_ratio - 1 || Y != V_ratio - 1)) { ChangeColor(image, vec, x, y); From 6e5047f64ace5e842a082c7af47e77646b74e552 Mon Sep 17 00:00:00 2001 From: sinakratos Date: Tue, 14 Jun 2022 17:10:18 +0430 Subject: [PATCH 4/4] Added Makefile and changed struct name --- Makefile | 9 +++++++++ main.cpp | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6ea5f49 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +CC = g++ +OPENCV = `pkg-config --cflags --libs opencv4` +LIBS = $(OPENCV) + +main: main.cpp + $(CC) -o $@ $^ $(LIBS) + +clean: + rm main \ No newline at end of file diff --git a/main.cpp b/main.cpp index e3d6ffc..11f08d4 100644 --- a/main.cpp +++ b/main.cpp @@ -15,7 +15,7 @@ int H_ratio, V_ratio; Mat image; // coordination struct to use in the callback function -struct initRoi { +struct Coordinates { bool init = false; // on off @@ -34,6 +34,7 @@ void startEndPoint(); // main int main() { + system("clear"); // getting the ratio of the output. (it's suggested to be a low number otherwise it will crash) @@ -51,8 +52,9 @@ int main() { startEndPoint(); imshow("Output", image); // show the output image setMouseCallback("Output", CallBackFunc); // a callback function - + waitKey(0); + return 0; } // this function has the algorithm for the rectangles to be made, and makes them @@ -154,4 +156,4 @@ void startEndPoint() { Scalar(0, 200, 0), FILLED); rectangle(image, Point(pixel * (H_ratio - 1), pixel * (V_ratio - 1)), Point(pixel * H_ratio, pixel * V_ratio), Scalar(0, 0, 0), 2, LINE_8); -} \ No newline at end of file +}