OPencv掩膜操作

掩膜操作作用

1.提取感興趣區(qū),用預先制作的感興趣區(qū)掩模與待處理圖像相乘,得到感興趣區(qū)圖像,感興趣區(qū)內(nèi)圖像值保持不變,而區(qū)外圖像值都為0。

2.屏蔽作用,用掩模對圖像上某些區(qū)域作屏蔽,使其不參加處理或不參加處理參數(shù)的計算,或僅對屏蔽區(qū)作處理或統(tǒng)計。

3.結(jié)構(gòu)特征提取,用相似性變量或圖像匹配方法檢測和提取圖像中與掩模相似的結(jié)構(gòu)特征。

4.特殊形狀圖像的制作。

以提高對比度為例



Mat src, dst;

src = imread("E:/CPLusProject/OpencvPeoject/FirstOpen2/FirstOpen2/dog.jpg");

if (!src.data) {

????????printf("could not load image...\n");

????????return -1;

}

namedWindow("input image", WINDOW_AUTOSIZE);

imshow("input image", src);

int cols = (src.cols - 1) * src.channels();

int offsetx = src.channels();

int rows = src.rows;

//創(chuàng)建一個純黑色圖

dst = Mat::zeros(src.size(), src.type());

for (int row = 1; row < (rows - 1); row++) {

????????????const uchar* previous = src.ptr<uchar>(row - 1);

????????????const uchar* current = src.ptr<uchar>(row);

????????????const uchar* next = src.ptr<uchar>(row + 1);

????????????uchar* output = dst.ptr<uchar>(row);

????????????for (int col = offsetx; col < cols; col++) {

????????????????????output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));

????????????}

}

namedWindow("contrast image demo", WINDOW_AUTOSIZE);

imshow("contrast image demo", dst);

//內(nèi)置掩膜API

Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);

filter2D(src, dst, src.depth(), kernel);

namedWindow("contrast image demo2", WINDOW_AUTOSIZE);

imshow("contrast image demo2", dst);

waitKey(0);


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容