I call a function rgbTOgray and trying to copy each image in a "vector of images" (which is a vector of colored images) to a temporary cv::Mat using a for loop. And, convert it to grayscale using cvtColor. Here is the code:
std::vector<cv::Mat> rgbTOgray(std::vector<cv::Mat>& rgb_image)
{
cv::Mat temp_gray, temp_rgb;
std::vector<cv::Mat> gray_image;
std::vector<cv::Mat>::iterator itr;
for ( itr = rgb_image.begin() ; itr < rgb_image.end() ; itr++)
{
temp_rgb = *itr;
cvtColor(temp_rgb, temp_gray, CV_RGB2GRAY);
gray_image.push_back(temp_gray);
}
return gray_image;
}
EDIT: I am returning a vector of Gray-scale images "gray_image". But this doesn't seem to work.
EDIT_2: OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor
Could anybody please help me out? I am new and learning all this stuff. Thanks!
You are not storing in vector gray_image
the gray images, but cv::Mat
headers with pointers to a single block of memory data which is firstly allocated by temp_gray
. You must clone those data not to overwrite your images:
gray_image.push_back(temp_gray.clone());
By the way, I would add gray_image.reserve(rgb_image.size());
before your loop.
One problem is that you are forgetting that cv::Mat
is reference counted with a "copy" not really copying anything, just a pointer.
The push_back(temp_grey)
keeps pushing the same image into your array.
You should declare cv::Mat temp_gray
inside the loop to make sure you get fresh copies for each element.
Also, check the number of channels of your "rgb" images ( temp_rgb.channels()
). The assert basically says that they are not 3/4 channels.
I do't know exactly why (I'm a learner too) but in my case it works only with .clone()
when you push back the temp image just gray-convert.
Anyway, the iterator line in your code is correct.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
std::vector<cv::Mat> rgb2gray(std::vector<cv::Mat>& rgb_image)
{
cv::Mat temp_gray;
std::vector<cv::Mat> gray_image;
std::vector<cv::Mat>::iterator itr;
gray_image.reserve(rgb_image.size());
for ( itr = rgb_image.begin() ; itr < rgb_image.end() ; itr++)
{
cvtColor(*itr, temp_gray, CV_RGB2GRAY);
gray_image.push_back(temp_gray.clone());
}
return gray_image;
}
int main(int /*argc*/, char** /*argv*/) {
Mat imgA, imgB;
imgA = imread("test1.jpg");
imgB = imread("test2.jpg");
vector<Mat> input;
input.push_back(imgA);
input.push_back(imgB);
vector<Mat> output;
output = rgb2gray(input);
stringstream ss;
for(size_t i = 0; i<input.size(); i++) {
ss.str(""); ss << "in" << i;
imshow( ss.str(), input[i] );
ss.str(""); ss << "out" << i;
imshow( ss.str(), output[i] );
}
while( true ) {
char c = (char)waitKey(10);
if( c == 27 )
{ break; }
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.