c++ - Gamma Correction with pow -
i use gamma correction image. so, have pow every pixel intensity of source image g = 0.6. have problem cause destination image wrong. maybe have casting problem when take pixel source image. here code:
#include <opencv2/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; using namespace std; int main() { mat src = imread("spine1.jpeg"); mat dst = mat(src.rows, src.cols, cv_8uc1); cvtcolor(src, src, cv_8uc1); dst = scalar(0); (int x = 0; x < src.rows; x++) { (int y = 0; y < src.cols; y++) { int pixelvalue = (int)src.at<uchar>(x, y); dst.at<uchar>(x, y) = pow(pixelvalue, 0.6); } } namedwindow("input", cv_window_autosize); namedwindow("output", cv_window_autosize); imshow("input", src); imshow("output", dst); waitkey(0); return 0; }
edit: change cvtcolor(src, src, cv_8uc1);
in cvtcolor(src, src, color_bgr2gray);
the call cvtcolor
wrong. should use:
cvtcolor(src, src, color_bgr2gray);
also, can make code simpler, , less error prone:
#include <opencv2/opencv.hpp> int main() { // load image grayscale cv::mat1b src = cv::imread("path_to_img", cv::imread_grayscale); // convert double "pow" cv::mat1d dsrc; src.convertto(dsrc, cv_64f); // compute "pow" cv::mat1d ddst; cv::pow(dsrc, 0.6, ddst); // convert uchar cv::mat1b dst; ddst.convertto(dst, cv_8u); // show results imshow("src", src); imshow("dst", dst); waitkey(); return 0; }
Comments
Post a Comment