我一直在寻找使用OpenCV分隔RGB层和只提取图像的绿色层的方法,但是当我想运行从代码生成的可执行文件时,我得到了以下错误:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
using namespace cv;
using namespace std;
Mat img, g, fin_img;
img = imread("img.png",CV_LOAD_IMAGE_COLOR);
vector<Mat> channels(3);
g = Mat::zeros(Size(img.rows, img.cols), img.type());
channels.push_back(g);
channels.push_back(g);
channels.push_back(img);
merge(channels, fin_img);
imshow("img", fin_img);
waitKey(0);
return 0;
错误是:
OpenCV错误:合并中的断言失败(mvi.size == mv.size && mvi.depth() ==深度),文件/opt/opencv/模块/core/src/Convert.cpp,第336行在抛出函数合并中的'cv::Exception‘():/opt/opencv/modules/core/src/ ==:336: error:(215) mvi.size == mv.size && mvi.depth() ==深度后调用
发布于 2017-11-01 18:00:44
您应该使用拆分函数
Mat a = imread(path);
Mat channels[3];
split(a, channels);
imshow("Green", channels[1]);
发布于 2017-11-01 18:06:34
首先创建一个包含3个空矩阵的向量,然后再向后推3个其他矩阵,从而生成一个由6个不同大小的矩阵组成的向量。
简单地创建一个空向量开始:
vector<Mat> channels;
g = Mat::zeros(Size(img.rows, img.cols), img.type());
channels.push_back(g);
channels.push_back(g);
channels.push_back(img);
然而,你所做的不是提取绿色通道。为此,您需要split
和merge
,或者您可以使用简历:提取渠道
cv::Mat green;
cv::extractChannel(image, green, 1);
发布于 2017-11-01 17:07:24
(猜测你给出的大块头不会编译)
Mat black = Mat::zeros(rows, cols, src.type());
似乎是错误的,如果输入图像有3个通道,您需要这里的单个通道,因此不能在这里使用src.type()
。
https://stackoverflow.com/questions/47059994
复制相似问题