#include #include #include #include #include using namespace cv; // input files char* inputBoardBMP = "chessboard.bmp"; char* inputBoardJPG = "chessboard.jpg"; char* inputBoardPNG = "chessboard.png"; char* inputBoardTIF = "chessboard.tif"; // Mats Mat originalImg, greyImg, threshImg; Mat blackSquares, whiteSquares; Mat finishedImg; // Thresholds /** * range: 0-255 */ int thresholdValue = 0; /** * 0: binary * 1: binary-inverted * 2: truncate * 3: to-zero * 4: to-zero-inverted */ int thresholdType = 0; int const MAX_TYPE = 4; int const MAX_BINARY = 255; // Window Names char* colorWindow = "Color Image"; char* greyWindow = "Grey Image"; char* threshWindow = "Threshold Image"; char* whiteSquareWindow = "White Squares"; char* blackSquareWindow = "Black Squares"; // Function Prototypes void Threshold_Demo(int, void*); void Colorize_Demo(int, void*); int main() { /** * Type: * CV_LOAD_IMAGE_UNCHANGED: include transparency (alpha) channel * CV_LOAD_IMAGE_GRAYSCALE: load as single-channel * CV_LOAD_IMAGE_COLOR: load as BGR */ originalImg = imread(inputBoardJPG, IMREAD_COLOR); // ensure image was valid if(!originalImg.data) { std::cout << "Missing data." << std::endl; return -1; } // display the image namedWindow(colorWindow, WINDOW_AUTOSIZE); imshow(colorWindow, originalImg); // wait for user to press any key waitKey(0); // greyscale the image cvtColor(originalImg, greyImg, COLOR_BGR2GRAY); // display the grey image namedWindow(greyWindow, WINDOW_AUTOSIZE); imshow(greyWindow, greyImg); // wait for user to press any key waitKey(0); // close the windows //cvDestroyWindow(colorWindow); destroyWindow(greyWindow); namedWindow(threshWindow, WINDOW_NORMAL); // create type trackbar createTrackbar("Type ", threshWindow, &thresholdType, MAX_TYPE, Threshold_Demo); // create value trackbar createTrackbar("Value ", threshWindow, &thresholdValue, MAX_BINARY, Threshold_Demo); Threshold_Demo(0, 0); waitKey(0); destroyWindow(threshWindow); if(thresholdType == 1) { blackSquares = threshImg.clone(); bitwise_not(blackSquares, whiteSquares); } else if(thresholdType == 0) { whiteSquares = threshImg.clone(); bitwise_not(whiteSquares, blackSquares); } else { destroyWindow(colorWindow); return 0; } namedWindow(whiteSquareWindow, WINDOW_AUTOSIZE); imshow(whiteSquareWindow, blackSquares); waitKey(0); destroyWindow(whiteSquareWindow); namedWindow(blackSquareWindow, WINDOW_AUTOSIZE); imshow(blackSquareWindow, whiteSquares); waitKey(0); destroyWindow(blackSquareWindow); destroyWindow(colorWindow); return 1; } void Threshold_Demo(int, void*) { // (src, dst, threshold value, max, type of threshold) threshold(greyImg, threshImg, thresholdValue, MAX_BINARY, thresholdType); imshow(threshWindow, threshImg); }