[英]OpenCV filter2D inside non rectangular ROI
I have an image in which only a small non-rectangular portion is useful. 我有一个图像,其中只有很小的非矩形部分有用。 I have a binary mask indicating the useful ROI.
我有一个二进制掩码表示有用的投资回报率。
How can I apply cv::filter2D
in OpenCV to that ROI only, defined by the binary mask? 如何将OpenCV中的
cv::filter2D
仅应用于由二进制掩码定义的ROI?
Edit 编辑
My pixels outside the ROI have a value of 0. The other have float values of around 300-500, so the problem with filter2D in the borders of the ROI have high value transitions. 我在ROI之外的像素的值为0。另一个像素的浮点值为300-500,因此ROI边界中filter2D的问题具有较高的值过渡。
It would also be acceptable to just set the pixel values outside the ROI as the nearest pixel inside the ROI, something similar to cv::BORDER_REPLICATE
将ROI外部的像素值设置为ROI内部最接近的像素也是可以接受的,类似于
cv::BORDER_REPLICATE
May be like this. 可能是这样。 I think there is no problem near border mask
我认为边界面具附近没有问题
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
Mat m = imread("f:/lib/opencv/samples/data/lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat mask=Mat::zeros(m.size(), CV_8UC1),maskBlur,mc;
// mask is a disk
circle(mask, Point(200, 200), 100, Scalar(255),-1);
Mat negMask;
// neg mask
bitwise_not(mask, negMask);
circle(mask, Point(200, 200), 100, Scalar(255), -1);
Mat md,mdBlur,mdint;
m.copyTo(md);
// All pixels outside mask set to 0
md.setTo(0, negMask);
imshow("mask image", md);
// Convert image to int
md.convertTo(mdint, CV_32S);
Size fxy(13, 13);
blur(mdint, mdBlur, fxy);
mdBlur.convertTo(mc, CV_8U);
imshow("Blur without mask", mc);
imwrite("blurwithoutmask.jpg",mc);
mask.convertTo(maskBlur, CV_32S);
// blur mask
blur(maskBlur, maskBlur, fxy);
Mat mskB;
mskB.setTo(1, negMask);
divide(mdBlur,maskBlur/255,mdBlur);
mdBlur.convertTo(mc, CV_8U);
imshow("Blur with mask", mc);
imwrite("blurwithmask.jpg",mc);
waitKey();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.