首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >分色组分

分色组分
EN

Stack Overflow用户
提问于 2014-01-17 10:37:52
回答 3查看 1.4K关注 0票数 1

我正在跟踪的帖子,以找到一个图像的颜色成分。我想我先从红色成分开始,然后再转向其他组件。下面给出了我编写的代码。我不是使用Linux,而是使用Windows。

代码语言:javascript
运行
复制
#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类型的实体。

我哪里出问题了?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-17 12:08:37

使用C++:

代码语言:javascript
运行
复制
cv::Mat myImage;
myImage=imread("myImage.jpg");
std::vector<cv::Mat> channels;
cv::split(myImage,channels);
imshow("Red Channel",channels[2]);
票数 4
EN

Stack Overflow用户

发布于 2014-01-17 10:44:41

这里定义了red

IplImage* red=cvCreateImage(...

看起来您只是想要一个单独的颜色值,所以您需要使用一个新变量(我把它命名为redValue,以减少混淆)。

代码语言:javascript
运行
复制
    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;
    }
票数 0
EN

Stack Overflow用户

发布于 2014-01-17 10:52:24

首先:如果您没有被迫使用IplImage,请切换到cv::Mat,因为它非常容易使用和理解openCV的新c++语法!

针对您的问题:如果您只想提取红色通道(像手动提取一样,openCV可以为您的函数调用),请尝试下面的代码:我添加了一些注释,其中我做了一些更改:

我使用它作为测试的输入映像:

代码语言:javascript
运行
复制
#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中显示它并关闭其他通道,请尝试如下:

代码语言:javascript
运行
复制
#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输入图像的结果如下所示:

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21183766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档