I have a binary mask that I want to be permanently applied to a colour image how would I do this? The binary mask should be preferably permanent- as in I don't want to reapply the mask to the image every time I call a function.
我有一个二进制掩码,我想永久应用于彩色图像我该怎么做?二进制掩码应该是永久性的 - 因为我不希望每次调用函数时都将掩码重新应用于图像。
Basic Code Examples would be appreciated. If you use code, please explain the code instead of just posting it.
基本代码示例将不胜感激。如果您使用代码,请解释代码而不是发布代码。
Thank You
5 个解决方案
#1
8
You don't apply a binary mask to an image. You (optionally) use a binary mask in a processing function call to tell the function which pixels of the image you want to process. If I'm completely misinterpreting your question, you should add more detail to clarify.
您不会将二进制掩码应用于图像。您(可选)在处理函数调用中使用二进制掩码来告诉函数您要处理的图像的像素。如果我完全误解了你的问题,你应该添加更多细节来澄清。
#2
46
While @perrejba s answer is correct, it uses the legacy C-style functions. As the question is tagged C++, you may want to use a method instead:
虽然@perrejba的答案是正确的,但它使用了传统的C风格函数。由于问题标记为C ++,您可能希望使用方法:
inputMat.copyTo(outputMat, maskMat);
All objects are of type cv::Mat
.
所有对象都是cv :: Mat类型。
Please be aware that the masking is binary. Any non-zero value in the mask is interpreted as 'do copy'. Even if the mask is a greyscale image.
请注意,屏蔽是二进制的。掩码中的任何非零值都被解释为“do copy”。即使蒙版是灰度图像。
Also be aware that the .copyTo() function does not clear the output before copying.
还要注意.copyTo()函数在复制之前不会清除输出。
If you want to permanently alter the original Image, you have to do an additional copy/clone/assignment. The copyTo() function is not defined for overlapping input/output images. So you can't use the same image as both input and output.
如果要永久更改原始图像,则必须执行其他复制/克隆/分配。未为重叠输入/输出图像定义copyTo()函数。因此,您不能使用相同的图像作为输入和输出。
#3
5
You can use the mask to copy only the region of interest of an original image to a destination one:
您可以使用蒙版仅将原始图像的感兴趣区域复制到目标区域:
cvCopy(origImage,destImage,mask);
where mask
should be an 8-bit single channel array.
其中mask应该是一个8位单通道数组。
See more at the OpenCV docs
有关OpenCV文档的更多信息,请参阅
#4
1
Well, this question appears on top of search results, so I believe we need code example here. Here's the Python code:
好吧,这个问题出现在搜索结果的顶部,所以我相信我们需要代码示例。这是Python代码:
import cv2
def apply_mask(frame, mask):
"""Apply binary mask to frame, return masked image.
"""
return cv2.bitwise_and(frame, frame, mask=mask)
Mask and frame must be the same size, so pixels remain as-is where mask is 1
and are set to zero where mask pixel is 0
.
掩码和帧必须具有相同的大小,因此像素保持原样,其中掩码为1,并且掩码像素为0时设置为零。
And for C++
it's a little bit different:
对于C ++,它有点不同:
cv::Mat inFrame; // Original (non-empty) image
cv::Mat mask; // Original (non-empty) mask
// ...
cv::Mat outFrame; // Result output
inFrame.copyTo(outFrame, mask);
#5
0
Here is some code to apply binary mask on a video frame sequence acquired from a webcam. comment and uncomment the "bitwise_not(Mon_mask,Mon_mask);"line and see the effect.
以下是在从网络摄像头获取的视频帧序列上应用二进制掩码的一些代码。注释并取消注释“bitwise_not(Mon_mask,Mon_mask);”行并查看效果。
bests, Ahmed.
#include "cv.h" // include it to used Main OpenCV functions.
#include "highgui.h" //include it to use GUI functions.
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
int c;
int radius=100;
CvPoint2D32f center;
//IplImage* color_img;
Mat image, image0,image1;
IplImage *tmp;
CvCapture* cv_cap = cvCaptureFromCAM(0);
while(1) {
tmp = cvQueryFrame(cv_cap); // get frame
// IplImage to Mat
Mat imgMat(tmp);
image =tmp;
center.x = tmp->width/2;
center.y = tmp->height/2;
Mat Mon_mask(image.size(), CV_8UC1, Scalar(0,0,0));
circle(Mon_mask, center, radius, Scalar(255,255,255), -1, 8, 0 ); //-1 means filled
bitwise_not(Mon_mask,Mon_mask);// commenté ou pas = RP ou DMLA
if(tmp != 0)
imshow("Glaucom", image); // show frame
c = cvWaitKey(10); // wait 10 ms or for key stroke
if(c == 27)
break; // if ESC, break and quit
}
/* clean up */
cvReleaseCapture( &cv_cap );
cvDestroyWindow("Glaucom");
}
#1
8
You don't apply a binary mask to an image. You (optionally) use a binary mask in a processing function call to tell the function which pixels of the image you want to process. If I'm completely misinterpreting your question, you should add more detail to clarify.
您不会将二进制掩码应用于图像。您(可选)在处理函数调用中使用二进制掩码来告诉函数您要处理的图像的像素。如果我完全误解了你的问题,你应该添加更多细节来澄清。
#2
46
While @perrejba s answer is correct, it uses the legacy C-style functions. As the question is tagged C++, you may want to use a method instead:
虽然@perrejba的答案是正确的,但它使用了传统的C风格函数。由于问题标记为C ++,您可能希望使用方法:
inputMat.copyTo(outputMat, maskMat);
All objects are of type cv::Mat
.
所有对象都是cv :: Mat类型。
Please be aware that the masking is binary. Any non-zero value in the mask is interpreted as 'do copy'. Even if the mask is a greyscale image.
请注意,屏蔽是二进制的。掩码中的任何非零值都被解释为“do copy”。即使蒙版是灰度图像。
Also be aware that the .copyTo() function does not clear the output before copying.
还要注意.copyTo()函数在复制之前不会清除输出。
If you want to permanently alter the original Image, you have to do an additional copy/clone/assignment. The copyTo() function is not defined for overlapping input/output images. So you can't use the same image as both input and output.
如果要永久更改原始图像,则必须执行其他复制/克隆/分配。未为重叠输入/输出图像定义copyTo()函数。因此,您不能使用相同的图像作为输入和输出。
#3
5
You can use the mask to copy only the region of interest of an original image to a destination one:
您可以使用蒙版仅将原始图像的感兴趣区域复制到目标区域:
cvCopy(origImage,destImage,mask);
where mask
should be an 8-bit single channel array.
其中mask应该是一个8位单通道数组。
See more at the OpenCV docs
有关OpenCV文档的更多信息,请参阅
#4
1
Well, this question appears on top of search results, so I believe we need code example here. Here's the Python code:
好吧,这个问题出现在搜索结果的顶部,所以我相信我们需要代码示例。这是Python代码:
import cv2
def apply_mask(frame, mask):
"""Apply binary mask to frame, return masked image.
"""
return cv2.bitwise_and(frame, frame, mask=mask)
Mask and frame must be the same size, so pixels remain as-is where mask is 1
and are set to zero where mask pixel is 0
.
掩码和帧必须具有相同的大小,因此像素保持原样,其中掩码为1,并且掩码像素为0时设置为零。
And for C++
it's a little bit different:
对于C ++,它有点不同:
cv::Mat inFrame; // Original (non-empty) image
cv::Mat mask; // Original (non-empty) mask
// ...
cv::Mat outFrame; // Result output
inFrame.copyTo(outFrame, mask);
#5
0
Here is some code to apply binary mask on a video frame sequence acquired from a webcam. comment and uncomment the "bitwise_not(Mon_mask,Mon_mask);"line and see the effect.
以下是在从网络摄像头获取的视频帧序列上应用二进制掩码的一些代码。注释并取消注释“bitwise_not(Mon_mask,Mon_mask);”行并查看效果。
bests, Ahmed.
#include "cv.h" // include it to used Main OpenCV functions.
#include "highgui.h" //include it to use GUI functions.
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
int c;
int radius=100;
CvPoint2D32f center;
//IplImage* color_img;
Mat image, image0,image1;
IplImage *tmp;
CvCapture* cv_cap = cvCaptureFromCAM(0);
while(1) {
tmp = cvQueryFrame(cv_cap); // get frame
// IplImage to Mat
Mat imgMat(tmp);
image =tmp;
center.x = tmp->width/2;
center.y = tmp->height/2;
Mat Mon_mask(image.size(), CV_8UC1, Scalar(0,0,0));
circle(Mon_mask, center, radius, Scalar(255,255,255), -1, 8, 0 ); //-1 means filled
bitwise_not(Mon_mask,Mon_mask);// commenté ou pas = RP ou DMLA
if(tmp != 0)
imshow("Glaucom", image); // show frame
c = cvWaitKey(10); // wait 10 ms or for key stroke
if(c == 27)
break; // if ESC, break and quit
}
/* clean up */
cvReleaseCapture( &cv_cap );
cvDestroyWindow("Glaucom");
}