生活随笔
收集整理的這篇文章主要介紹了
opencv直方图,lomo,cartoon
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直方圖繪制和直方圖均衡化、局部直方圖自適應均衡化
圖像LOMO效果(暗處更暗,亮處更亮,產生光暈,虛焦)
圖像cartoon效果
#include<opencv2/opencv.hpp>
#include<iostream>using namespace std
;
using namespace cv
;void lomo(string filename
);
void cartoon(string filename
);
void drawhist(string filename
);
int main()
{drawhist("dsn.tif");lomo("dsn.tif");cartoon("dsn.tif");waitKey(0);return 0;
}void drawhist(string filename
) {Mat src
= imread(filename
);if (src
.empty()) {return ;}vector
<Mat
> bgr
;split(src
, bgr
);Mat b
, g
, r
;b
= bgr
[0];g
= bgr
[1];r
= bgr
.at(2);Mat hist_b
, hist_g
, hist_r
;float range
[] = { 0,256 };int histsize
= 256;const float*ranges
= { range
};calcHist(&b
, 1, 0, Mat(), hist_b
, 1, &histsize
, &ranges
);calcHist(&g
, 1, 0, Mat(), hist_g
, 1, &histsize
, &ranges
);calcHist(&r
, 1, 0, Mat(), hist_r
, 1, &histsize
, &ranges
);int height
= 300, width
= 500;Mat
histImg(height
, width
, CV_8UC3
, Scalar(0, 0, 0));Mat
histImg_b(height
, width
, CV_8UC3
, Scalar(0, 0, 0));Mat
histImg_g(height
, width
, CV_8UC3
, Scalar(0, 0, 0));Mat
histImg_r(height
, width
, CV_8UC3
, Scalar(0, 0, 0));normalize(hist_b
, hist_b
, 0, histImg
.rows
, NORM_MINMAX
, -1, Mat());normalize(hist_g
, hist_g
, 0, histImg
.rows
, NORM_MINMAX
, -1, Mat());normalize(hist_r
, hist_r
, 0, histImg
.rows
, NORM_MINMAX
, -1, Mat());int bin_w
= cvRound((double)width
/ height
);for (int i
= 1; i
< hist_b
.rows
; i
++) {line(histImg_b
, Point((i
- 1)*bin_w
, height
- cvRound(hist_b
.at
<float>(i
- 1, 0))), Point((i
)*bin_w
, height
- cvRound(hist_b
.at
<float>(i
, 0))),Scalar(255, 0, 0), bin_w
);line(histImg_g
, Point((i
- 1)*bin_w
, height
- cvRound(hist_g
.at
<float>(i
- 1, 0))), Point((i
)*bin_w
, height
- cvRound(hist_g
.at
<float>(i
, 0))),Scalar(0, 255, 0), bin_w
);line(histImg_r
, Point((i
- 1)*bin_w
, height
- cvRound(hist_r
.at
<float>(i
- 1, 0))), Point((i
)*bin_w
, height
- cvRound(hist_r
.at
<float>(i
, 0))),Scalar(0, 0, 255), bin_w
);}histImg
= histImg_b
+ histImg_g
+ histImg_r
;Mat grayImg
, dst
;cvtColor(src
, grayImg
, COLOR_BGR2GRAY
);equalizeHist(grayImg
, dst
);cv
::Ptr
<cv
::CLAHE
> clahe
= createCLAHE(100, Size(10, 10));Mat jubudst
;clahe
->apply(grayImg
, jubudst
);waitKey(0);
}void lomo(string filename
){Mat src
= imread(filename
);if (src
.empty()) {return ;}const double exponential_e
= std
::exp(1.0);Mat
lut(1, 256, CV_8UC1
);Mat result
;for (int i
= 0; i
< 256; i
++) {double x
= (double)(i
/ 256.0);lut
.at
<uchar
>(i
) = cvRound(256 * (1 / (1 + pow(exponential_e
, -((x
- 0.5) / 0.1)))));}std
::vector
<Mat
> bgr
;split(src
, bgr
);cv
::LUT(bgr
[2], lut
, bgr
[2]);cv
::merge(bgr
, result
);Mat
halo(src
.size(), CV_32FC3
, Scalar(0.3, 0.3, 0.3));circle(halo
, Point(src
.cols
/ 2, src
.rows
/ 2), src
.cols
/ 3, Scalar(1, 1, 1), -1);blur(halo
, halo
, Size(src
.cols
/ 3, src
.rows
/ 3));Mat resultf
;result
.convertTo(resultf
, CV_32FC3
);cv
::multiply(resultf
, halo
, resultf
);resultf
.convertTo(result
, CV_8UC3
);waitKey(0);
}void cartoon(string filename
) {Mat src
= imread(filename
);if (src
.empty()) {return;}Mat imgMedian
;medianBlur(src
, imgMedian
, 7);Mat imgCanny
;Canny(imgMedian
, imgCanny
, 50, 150);Mat kernel
= getStructuringElement(MORPH_RECT
, Size(2, 2));dilate(imgCanny
, imgCanny
, kernel
);imgCanny
= imgCanny
/ 255;imgCanny
= 1 - imgCanny
;Mat imgCannyf
;imgCanny
.convertTo(imgCannyf
, CV_32FC3
);blur(imgCannyf
, imgCannyf
, Size(5, 5));Mat imgBF
;bilateralFilter(src
, imgBF
, 9, 150.0, 150.0);Mat result
= imgBF
/ 25;result
= result
* 25;Mat imgCanny3c
;Mat cannyChannels
[] = { imgCannyf
, imgCannyf
, imgCannyf
};merge(cannyChannels
, 3, imgCanny3c
);Mat resultf
;result
.convertTo(resultf
, CV_32FC3
);multiply(resultf
, imgCanny3c
, resultf
);resultf
.convertTo(result
, CV_8UC3
);waitKey(0);
}
總結
以上是生活随笔為你收集整理的opencv直方图,lomo,cartoon的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。