1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv;
Mat thresholdImage(const Mat& inputImage, int thresholdValue) { Mat grayImage, binaryImage; cvtColor(inputImage, grayImage, COLOR_BGR2GRAY); threshold(grayImage, binaryImage, thresholdValue, 255, THRESH_BINARY); return binaryImage; }
Mat thresholdImage2(const Mat& inputImage, int thresholdValue) { Mat grayImage; cvtColor(inputImage, grayImage, COLOR_BGR2GRAY); Mat binaryImage = grayImage.clone(); for (int i = 0; i < grayImage.rows; i++) { for (int j = 0; j < grayImage.cols; j++) { if (grayImage.at<uchar>(i, j) > thresholdValue) { binaryImage.at<uchar>(i, j) = 255; } else { binaryImage.at<uchar>(i, j) = 0; } } } return binaryImage; }
Mat logTransform(const Mat& inputImage, double c) { Mat resultImage = inputImage.clone(); resultImage.convertTo(resultImage, CV_32F); resultImage = resultImage + 1; log(resultImage, resultImage); resultImage = c * resultImage; normalize(resultImage, resultImage, 0, 255, NORM_MINMAX); resultImage.convertTo(resultImage, CV_8U); return resultImage; }
Mat gammaTransform(const Mat& inputImage, double gamma, double c) { Mat resultImage = inputImage.clone(); resultImage.convertTo(resultImage, CV_32F); pow(resultImage, gamma, resultImage); resultImage = c * resultImage; normalize(resultImage, resultImage, 0, 255, NORM_MINMAX); resultImage.convertTo(resultImage, CV_8U); return resultImage; }
Mat colorComplementTransform(const Mat& inputImage) { Mat resultImage = inputImage.clone(); resultImage = Scalar(255,255,255) - resultImage; return resultImage; }
int main() {
Mat myImage1 = imread("test.jpg");
Mat grayImage1; cvtColor(myImage1, grayImage1, COLOR_BGR2GRAY); imshow("grayImage", grayImage1); imwrite("grayImage.jpg", grayImage1);
int thresholdValue = 128; Mat binaryImage1 = thresholdImage2(myImage1, thresholdValue); imshow("binaryImage",binaryImage1); imwrite("binaryImage.jpg", binaryImage1);
double logC = 1; Mat logImage1 = logTransform(grayImage1, logC); imshow("logImage", logImage1); imwrite("logImage.jpg", logImage1);
double gamma = 2; double gammaC = 1; Mat gammaImage1 = gammaTransform(grayImage1, gamma, gammaC); imshow("gammaImage", gammaImage1); imwrite("gammaImage.jpg", gammaImage1);
Mat colorComplementImage1 = colorComplementTransform(myImage1); imshow("colorComplementImage", colorComplementImage1); imwrite("colorComplementImage.jpg", colorComplementImage1);
waitKey(0); return 0; }
|