這篇文章主要介紹了opencv3/C++圖像像素操作的方法的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇opencv3/C++圖像像素操作的方法文章都會有所收獲,下面我們一起來看看吧。
為企業(yè)提供成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)營銷推廣、競價托管、品牌運營等營銷獲客服務(wù)。創(chuàng)新互聯(lián)建站擁有網(wǎng)絡(luò)營銷運營團隊,以豐富的互聯(lián)網(wǎng)營銷經(jīng)驗助力企業(yè)精準獲客,真正落地解決中小企業(yè)營銷獲客難題,做到“讓獲客更簡單”。自創(chuàng)立至今,成功用技術(shù)實力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營銷”三大難題,同時降低了營銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認可!RGB圖像轉(zhuǎn)灰度圖
RGB圖像轉(zhuǎn)換為灰度圖時通常使用:
進行轉(zhuǎn)換,以下嘗試通過其他對圖像像素操作的方式將RGB圖像轉(zhuǎn)換為灰度圖像。
#include#include using namespace cv; int main() { //像素操作 Mat src,dst; src = imread("E:/image/image/daibola.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input"); imshow("input",src); dst.create(src.size(), src.type()); for(int row = 0; row < src.rows; row++) { for(int col = 0; col < src.cols; col++) { int b = src.at (row, col)[0]; int g = src.at (row, col)[1]; int r = src.at (row, col)[2]; dst.at (row, col)[0] = max(r,max(g,b)); dst.at (row, col)[1] = max(r,max(g,b)); dst.at (row, col)[2] = max(r,max(g,b)); } } namedWindow("output"); imshow("output",dst); waitKey(); }
同理使用min(r,min(g,b))可以看到由于選擇了較小的灰度值圖像會明顯變暗:
圖像線性增強
通過對圖像像素操作(線性變換),實現(xiàn)圖像的線性增強。
#include#include using namespace cv; int main() { Mat src1, dst; src1 = imread("E:/image/image/im1.jpg"); if(src1.empty()) { printf("can not load im1 \n"); return -1; } double alpha = 1.2, beta = 50; dst = Mat::zeros(src1.size(), src1.type()); for(int row = 0; row < src1.rows; row++) { for(int col = 0; col < src1.cols; col++) { if(src1.channels() == 3) { int b = src1.at (row, col)[0]; int g = src1.at (row, col)[1]; int r = src1.at (row, col)[2]; dst.at (row, col)[0] = saturate_cast (b*alpha + beta); dst.at (row, col)[1] = saturate_cast (g*alpha + beta); dst.at (row, col)[2] = saturate_cast (r*alpha + beta); } else if (src1.channels() == 1) { float v = src1.at (row, col); dst.at (row, col) = saturate_cast (v*alpha + beta); } } } namedWindow("output",CV_WINDOW_AUTOSIZE); imshow("output", dst); waitKey(); return 0; }
掩膜操作調(diào)整圖像對比度
使用一個3×3掩模增強圖像對比度:
#include#include using namespace cv; int main() { Mat src, dst; src = imread("E:/image/image/daibola.jpg"); CV_Assert(src.depth() == CV_8U); if(!src.data) { printf("can not load image \n"); return -1; } src.copyTo(dst); for(int row = 1; row<(src.rows - 1); row++) { const uchar* previous = src.ptr (row - 1); const uchar* current = src.ptr (row); const uchar* next = src.ptr (row + 1); uchar* output = dst.ptr (row); for(int col = src.channels(); col < (src.cols - 1)*src.channels(); col++) { *output = saturate_cast (9 * current[col] - 2*previous[col] - 2*next[col] - 2*current[col - src.channels()] - 2*current[col + src.channels()]); output++; } } namedWindow("image", CV_WINDOW_AUTOSIZE); imshow("image",dst); waitKey(); return 0; }
像素重映射
利用cv::remap實現(xiàn)像素重映射;
cv::remap參數(shù)說明:
Remap( InputArray src,// 輸入圖像 OutputArray dst,// 輸出圖像 InputArray map1,// 映射表1(CV_32FC1/CV_32FC2) InputArray map2,// 映射表2(CV_32FC1/CV_32FC2) int interpolation,// 選擇的插值 int borderMode,// 邊界類型(BORDER_CONSTANT) const Scalar borderValue// 顏色 )
插值方法:
CV_INTER_NN =0, CV_INTER_LINEAR =1, CV_INTER_CUBIC =2, CV_INTER_AREA =3, CV_INTER_LANCZOS4 =4
通過像素重映射實現(xiàn)圖像垂直翻轉(zhuǎn):
#includeusing namespace cv; int main() { Mat src,dst; src = imread("E:/image/image/daibola.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input", CV_WINDOW_AUTOSIZE); imshow("input", src); Mat mapx,mapy; mapx.create(src.size(), CV_32FC1); mapy.create(src.size(), CV_32FC1); for(int row = 0; row < src.rows; row++) { for(int col = 0; col < src.cols; col++) { mapx.at (row, col) = col; mapy.at (row, col) = src.rows - row - 1; } } remap(src, dst, mapx, mapy, CV_INTER_NN, BORDER_CONSTANT, Scalar(0,255,255)); namedWindow("output", CV_WINDOW_AUTOSIZE); imshow("output",dst); waitKey(); return 0; }
關(guān)于“opencv3/C++圖像像素操作的方法”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“opencv3/C++圖像像素操作的方法”知識都有一定的了解,大家如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。