问题
I have color image in im
, i want to get the pixel value of 3 channels image using vec3b using the following code
for (int i = 0; i < im.rows; i++)
{
for (int j = 0; j < im.cols; j++)
{
for (int k = 0; k < nChannels; k++)
{
zay[k] = im.at<Vec3b>(i, j)[k]; //get the pixel value and assign to new vec3b variable zay
}
}
}
After that, i want to multiply the following mat 3x3 filter with that vec3b in zay
Filter= (Mat_<double>(3, 3) << 0, 0, 0,
0, 1, 0,
0, 0, 0);
How to convert vec3b into mat matrix so i can multiplied with mat Filter?? And vec3b is an array 3x1? Thanks
回答1:
Didnt try but should work:
cv::Mat DoubleMatFromVec3b(cv::Vec3b in)
{
cv::Mat mat(3,1, CV_64FC1);
mat.at <double>(0,0) = in [0];
mat.at <double>(1,0) = in [1];
mat.at <double>(2,0) = in [2];
return mat;
};
回答2:
According to your example, what you are trying to accomplish is known as a convolution with a kernel.
You can set up the kernel and invoke cv::filter2D() to apply it for you:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main()
{
cv::Mat img = cv::imread("input.jpg");
if (img.empty())
{
std::cout << "!!! Failed to open input image" << std::endl;
return -1;
}
cv::Mat kernel = (cv::Mat_<float>(3, 3) << 0, 0, 0,
0, 1, 0,
0, 0, 0);
cv::Mat dst;
cv::filter2D(img, dst, -1, kernel, cv::Point(-1, -1), 0, cv::BORDER_DEFAULT);
cv::imshow("output", dst);
cv::waitKey(0);
return 0;
}
Thus, there's no need to iterate on the pixels and perform the computation yourself. OpenCV documentation explains it all at: Making your own linear filters!.
来源:https://stackoverflow.com/questions/27676352/convert-vec3b-into-mat