gpt4 book ai didi

c++ - OpenCV C++ 中的 mask 彩色图像

转载 作者:搜寻专家 更新时间:2023-10-31 01:40:14 24 4
gpt4 key购买 nike

我有一张黑白图像和一张相同大小的彩色图像。我想将它们组合起来得到一张黑色图像,其中黑白图像为黑色,而彩色图像与彩色图像颜色相同,其中黑白图像为白色。

这是 C++ 中的代码:

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
Mat img1 = imread("frame1.jpg"); //coloured image
Mat img2 = imread("framePr.jpg", 0); //grayscale image

imshow("Oreginal", img1);

//preform AND
Mat r;
bitwise_and(img1, img2, r);

imshow("Result", r);

waitKey(0);
return 0;

}

这是错误信息:

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array') in binary_op, file /home/voja/src/opencv-2.4.10/modules/core/src/arithm.cpp, line 1021
terminate called after throwing an instance of 'cv::Exception'
what(): /home/voja/src/opencv-2.4.10/modules/core/src/arithm.cpp:1021: error: (-209) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function binary_op

Aborted (core dumped)

最佳答案

首先,黑白(二值)图像不同于灰度图像。两者都是 CV_8U 类型的 Mat's。但是灰度图像中的每个像素都可以取 0 到 255 之间的任何值。二值图像应该只有两个值 - 一个零和一个非零数。

其次,bitwise_and 不能应用于不同类型的Mat's。灰度图像是 CV_8U 类型的单 channel 图像(每像素 8 位),彩色图像是 CV_BGRA 类型的 3 channel 图像(每像素 32 位)。

看来您尝试做的事情可以用面具来完成。

//threshold grayscale to binary image 
cv::threshold(img2 , img2 , 100, 255, cv::THRESH_BINARY);
//copy the color image with binary image as mask
img1.copyTo(r, img2);

关于c++ - OpenCV C++ 中的 mask 彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30065953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com