日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

opencv直方图,lomo,cartoon

發(fā)布時(shí)間:2023/12/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv直方图,lomo,cartoon 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

直方圖繪制和直方圖均衡化、局部直方圖自適應(yīng)均衡化
圖像LOMO效果(暗處更暗,亮處更亮,產(chǎn)生光暈,虛焦)
圖像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;//0:blue,1:green,2:redsplit(src, bgr);Mat b, g, r;b = bgr[0];g = bgr[1];r = bgr.at(2);Mat hist_b, hist_g, hist_r;//存放直方圖計(jì)算結(jié)果float range[] = { 0,256 };int histsize = 256;const float*ranges = { range };//直方圖數(shù)值計(jì)算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++) {//Point q((i - 1)*bin_w, height - cvRound(hist_b.at<float>(i - 1, 0)));//Point h((i)*bin_w, height - cvRound(hist_b.at<float>(i,0)));//line(histImg, q, h, Scalar(255, 0, 0));//畫線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;//直方圖均衡化和局部直方圖自適應(yīng)均衡化都需要的是當(dāng)通道的圖像Mat grayImg, dst;cvtColor(src, grayImg, COLOR_BGR2GRAY);//直方圖均衡化equalizeHist(grayImg, dst);//局部直方圖自適應(yīng)均衡化//createCLAHE(clipLimit:顏色對(duì)比度的閾值,titleGridSize:進(jìn)行像素均衡化的網(wǎng)格大小,即在多少網(wǎng)格下進(jìn)行直方圖的均衡操作//1、實(shí)例化自適應(yīng)直方圖均衡化函數(shù)cv::Ptr<cv::CLAHE> clahe = createCLAHE(100, Size(10, 10));//2、進(jìn)行自適應(yīng)直方圖均衡化Mat jubudst;clahe->apply(grayImg, jubudst);waitKey(0); }void lomo(string filename){Mat src = imread(filename);//Mat src = imread("lena.png");if (src.empty()) {return ;}//Mat src = Mat::zeros(Size(200, 200), CV_8UC3);//circle(src, Point(src.cols / 2, src.rows / 2), src.cols / 4, Scalar(255, 255, 255), -1);const double exponential_e = std::exp(1.0);Mat lut(1, 256, CV_8UC1);Mat result;//uchar* plut = lut.data;for (int i = 0; i < 256; i++) {double x = (double)(i / 256.0);//plut[i] = cvRound(256 * ( 1.0 / (1.0 + pow(exponential_e, -((x - 0.5) / 0.1))) ) );lut.at<uchar>(i) = cvRound(256 * (1 / (1 + pow(exponential_e, -((x - 0.5) / 0.1)))));//pow(InputArray src, double p, OutputArray dst)}//類似于根據(jù)新的計(jì)算公式,把原像素值 "映射" 到新像素值上去//2.分離通道std::vector<Mat> bgr;split(src, bgr);//3.調(diào)用LUT函數(shù)處理紅色通道//也可以是藍(lán)色,綠色cv::LUT(bgr[2], lut, bgr[2]);//cv::LUT(bgr[0], lut, bgr[0]);//cv::LUT(bgr[1], lut, bgr[1]);//4.合并通道cv::merge(bgr, result);//5.創(chuàng)建黑暗光環(huán)Mat halo(src.size(), CV_32FC3, Scalar(0.3, 0.3, 0.3));//這里畫光環(huán)寬度應(yīng)該是-1circle(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));//6.將光環(huán)放到原圖像上去,方法是將兩個(gè)圖像相乘(相乘必須是浮點(diǎn)型,所以需要類型轉(zhuǎn)換)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);//Mat src = imread("lena.png");if (src.empty()) {return;}Mat imgMedian;//1、中值濾波器去噪聲,根據(jù)噪聲特性medianBlur(src, imgMedian, 7);// Detect edges with canny//2、消除噪聲后,用canny算子檢測(cè)強(qiáng)邊緣Mat imgCanny;Canny(imgMedian, imgCanny, 50, 150);// Dilate(膨脹) the edges使斷續(xù)的邊緣連接Mat kernel = getStructuringElement(MORPH_RECT, Size(2, 2));dilate(imgCanny, imgCanny, kernel);// Scale edges values to 1 and invert values//為防止乘法溢出,先將邊緣圖像整合到0-1區(qū)間imgCanny = imgCanny / 255;imgCanny = 1 - imgCanny;// Use float values to allow multiply between 0 and 1//3、乘法(將邊緣圖像加到原圖像上(如lomo一樣))Mat imgCannyf;imgCanny.convertTo(imgCannyf, CV_32FC3);// Blur the edgest to do smooth effect平滑邊緣(小模糊)blur(imgCannyf, imgCannyf, Size(5, 5));/** COLOR **///顏色過(guò)濾// Apply bilateral filter to homogenizes color//為了得到卡通外觀使用bilateralFilter雙邊濾波濾鏡//一種濾波器,在保持邊緣的同時(shí)降低圖像的噪聲,通過(guò)適當(dāng)?shù)膮?shù)(輸入圖像,輸出圖像,像素領(lǐng)域直徑d,sigma色值,sigma坐標(biāo)空間)Mat imgBF;bilateralFilter(src, imgBF, 9, 150.0, 150.0);//當(dāng)直徑(第三個(gè)參數(shù))大于5的時(shí)候,bilateral開始變慢//當(dāng)sigma的值大于150的時(shí)候,會(huì)出現(xiàn)卡通效果// truncate colors//為了更強(qiáng)大的卡通效果,通過(guò)乘除像素值將可能的顏色值截?cái)酁?0Mat result = imgBF / 25;result = result * 25;/** MERGES COLOR + EDGES **/// Create a 3 channles for edges//必須合并顏色和邊緣結(jié)果,然后創(chuàng)建一個(gè)三通道圖像Mat imgCanny3c;Mat cannyChannels[] = { imgCannyf, imgCannyf, imgCannyf };merge(cannyChannels, 3, imgCanny3c);//合并// Convert color result to floatMat resultf;result.convertTo(resultf, CV_32FC3);// Multiply color and edges matricesmultiply(resultf, imgCanny3c, resultf);// convert to 8 bits colorresultf.convertTo(result, CV_8UC3);waitKey(0); }

總結(jié)

以上是生活随笔為你收集整理的opencv直方图,lomo,cartoon的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。