想要从图像中获取绿色 RGB 通道时出错
Error wanting to get green RGB channel from an image
我一直在寻找使用 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 Error: Assertion failed (mv[i].size == mv[0].size && mv[i].depth() == depth) in merge, file /opt/opencv/modules/core/src/convert.cpp, line 336
terminate called after throwing an instance of 'cv::Exception'
what(): /opt/opencv/modules/core/src/convert.cpp:336: error: (-215) mv[i].size == mv[0].size && mv[i].depth() == depth in function merge
(猜测是因为你给的 hunk 没有编译)
Mat black = Mat::zeros(rows, cols, src.type());
似乎不对,如果输入图像有3个通道,你在这里想要一个通道,因此src.type()
不能在这里使用。
您应该使用 split 函数
Mat a = imread(path);
Mat channels[3];
split(a, channels);
imshow("Green", channels[1]);
您首先创建一个包含 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::extractChannel:
cv::Mat green;
cv::extractChannel(image, green, 1);
我一直在寻找使用 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 Error: Assertion failed (mv[i].size == mv[0].size && mv[i].depth() == depth) in merge, file /opt/opencv/modules/core/src/convert.cpp, line 336 terminate called after throwing an instance of 'cv::Exception' what(): /opt/opencv/modules/core/src/convert.cpp:336: error: (-215) mv[i].size == mv[0].size && mv[i].depth() == depth in function merge
(猜测是因为你给的 hunk 没有编译)
Mat black = Mat::zeros(rows, cols, src.type());
似乎不对,如果输入图像有3个通道,你在这里想要一个通道,因此src.type()
不能在这里使用。
您应该使用 split 函数
Mat a = imread(path);
Mat channels[3];
split(a, channels);
imshow("Green", channels[1]);
您首先创建一个包含 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::extractChannel:
cv::Mat green;
cv::extractChannel(image, green, 1);