我正在跟踪这的帖子,以找到一个图像的颜色成分。我想我先从红色成分开始,然后再转向其他组件。下面给出了我编写的代码。我不是使用Linux,而是使用Windows。
#include <opencv\cv.h>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, char**argv)
{
IplImage* image=cvLoadImage("C:/Users/Administrator/Desktop/sample.png",1);
IplImage* red=cvCreateImage(cvSize(image->width, image->height), image->depth,image->nChannels);
uchar *pImg =(uchar*)image->imageData;
uchar *pRed=(uchar*)red->imageData;
for(int i=0;i<image->height;i++)
{
for(int j=0;j<image->width;j++)
{
red=pImg[i*image->widthStep + j*image->nChannels + 2];
pRed[i*image->widthStep + j*image->nChannels + 2]=red;
}
}
namedWindow("Display",1);
cvShowImage("Display",red);
waitKey(0);
return 0;
}
线
红色=pImgi*image->widthStep+ j*image->nChannels + 2; pRedi*image->widthStep + j*image->nChannels + 2=red;
正在显示此错误:
不能将uchar类型的值分配给IplImage类型的实体。
我哪里出问题了?
发布于 2014-01-17 12:08:37
使用C++:
cv::Mat myImage;
myImage=imread("myImage.jpg");
std::vector<cv::Mat> channels;
cv::split(myImage,channels);
imshow("Red Channel",channels[2]);
发布于 2014-01-17 10:44:41
这里定义了red
:
IplImage* red=cvCreateImage(
...
看起来您只是想要一个单独的颜色值,所以您需要使用一个新变量(我把它命名为redValue
,以减少混淆)。
for(int j=0;j<image->width;j++)
{
uchar redValue=pImg[i*image->widthStep + j*image->nChannels + 2];
pRed[i*image->widthStep + j*image->nChannels + 2]=redValue;
}
发布于 2014-01-17 10:52:24
首先:如果您没有被迫使用IplImage
,请切换到cv::Mat
,因为它非常容易使用和理解openCV的新c++语法!
针对您的问题:如果您只想提取红色通道(像手动提取一样,openCV可以为您的函数调用),请尝试下面的代码:我添加了一些注释,其中我做了一些更改:
我使用它作为测试的输入映像:
#include <opencv\cv.h>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, char**argv)
{
IplImage* image=cvLoadImage("C:/Users/Administrator/Desktop/sample.png",1);
// !!! the image that shall contain the red channel must have number of channels = 1 !!!
IplImage* red=cvCreateImage(cvSize(image->width, image->height), image->depth,1);
uchar *pImg =(uchar*)image->imageData;
uchar *pRed=(uchar*)red->imageData;
for(int i=0;i<image->height;i++)
{
for(int j=0;j<image->width;j++)
{
// !!! you have to use a variable that holds the value of single channel's pixel, which is uchar in this case (values from 0 to 255)
uchar redVal=pImg[i*image->widthStep + j*image->nChannels + 2];
// !!! since the red image has only 1 channel, be sure to use the right ->nChannels (from the 'red' image
pRed[i*red->widthStep + j*red->nChannels]=redVal;
}
}
namedWindow("Display",1);
cvShowImage("Display",red);
waitKey(0);
return 0;
}
这应该只显示红色通道为灰度图像。
lena输入图像的结果如下所示:
如果要在RGB中显示它并关闭其他通道,请尝试如下:
#include <opencv\cv.h>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc, char**argv)
{
IplImage* image=cvLoadImage("C:/Users/Administrator/Desktop/sample.png",1);
// !!! use 3 channels again because we want to display colors
IplImage* red=cvCreateImage(cvSize(image->width, image->height), image->depth,3);
uchar *pImg =(uchar*)image->imageData;
uchar *pRed=(uchar*)red->imageData;
for(int i=0;i<image->height;i++)
{
for(int j=0;j<image->width;j++)
{
// !!! you have to use a variable that holds the value of single channel's pixel, which is uchar in this case (values from 0 to 255)
uchar redVal=pImg[i*image->widthStep + j*image->nChannels + 2];
// !!! set all channels to zero, except the red channel which is copied. be sure to use the right widthStep
pRed[i*red->widthStep + j*red->nChannels + 0]=0;
pRed[i*red->widthStep + j*red->nChannels + 1]=0;
pRed[i*red->widthStep + j*red->nChannels + 2]=redVal;
}
}
namedWindow("Display",1);
cvShowImage("Display",red);
waitKey(0);
return 0;
}
lena输入图像的结果如下所示:
https://stackoverflow.com/questions/21183766
复制相似问题