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
|
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv;
cv::Mat logTransform1(cv::Mat srcImage, int c) { if (srcImage.empty()) std::cout << "No data!" << std::endl; cv::Mat resultImage = cv::Mat::zeros(srcImage.size(), srcImage.type()); cv::add(srcImage, cv::Scalar(1.0), srcImage); srcImage.convertTo(srcImage, CV_32F); log(srcImage, resultImage); resultImage = c * resultImage; cv::normalize(resultImage, resultImage, 0, 255, cv::NORM_MINMAX); cv::convertScaleAbs(resultImage, resultImage); return resultImage; }
cv::Mat logTransform2(Mat srcImage, float c) { if (srcImage.empty()) std::cout << "No data!" << std::endl; cv::Mat resultImage = cv::Mat::zeros(srcImage.size(), srcImage.type()); double gray = 0; for (int i = 0; i < srcImage.rows; i++) { for (int j = 0; j < srcImage.cols; j++) { gray = (double)srcImage.at<uchar>(i, j); gray = c * log((double)(1 + gray)); resultImage.at<uchar>(i, j) = saturate_cast<uchar>(gray); } } cv::normalize(resultImage, resultImage, 0, 255, cv::NORM_MINMAX); cv::convertScaleAbs(resultImage, resultImage); return resultImage; }
cv::Mat logTransform3(Mat srcImage, float c) { if (srcImage.empty()) std::cout << "No data!" << std::endl; cv::Mat resultImage = cv::Mat::zeros(srcImage.size(), srcImage.type()); srcImage.convertTo(resultImage, CV_32F); resultImage = resultImage + 1; cv::log(resultImage, resultImage); resultImage = c * resultImage; cv::normalize(resultImage, resultImage, 0, 255, cv::NORM_MINMAX); cv::convertScaleAbs(resultImage, resultImage); return resultImage; }
int main() { cv::Mat srcImage = cv::imread("lena.jpg", 0); if (!srcImage.data) return -1; cv::imshow("srcImage", srcImage); float c = 1.1; cv::Mat resultImage; resultImage = logTransform1(srcImage, c); cv::imshow("resultImage1", resultImage); resultImage = logTransform2(srcImage, c); cv::imshow("resultImage2", resultImage); resultImage = logTransform3(srcImage, c); cv::imshow("resultImage3", resultImage); cv::waitKey(0); return 0; }
|