实验1

#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;

void getChannel(const uchar *input, int width, int height,int inStep, int inChannels,uchar *output, int outStep, int channelToGet){
    for (int y = 0; y < height; ++y, input += inStep, output += outStep){
        const uchar *px = input;
        for (int x = 0; x < width; ++x, px += inChannels)
            output[x] = px[channelToGet];
    }
}
void getChannel(const Mat &in, Mat &output, int channelToGet)
{
    getChannel(in.data, in.cols, in.rows, in.step, in.channels(), output.data, output.step, channelToGet);
}

int main()
{
    Mat img = imread("f:/2008_000149.jpg", -1);

    Mat channel(img.size(), CV_8UC1);
    getChannel(img.data, img.cols, img.rows, img.step, img.channels(), channel.data, channel.step, 0);
    channel = 0;

    Rect roi(10, 10, 300, 300);
    getChannel(img.ptr(roi.y, roi.x), roi.width, roi.height, img.step, img.channels(),
    channel.ptr(roi.y, roi.x), channel.step, 0);
    getChannel(img(roi), channel(roi), 0);

    imshow("img", img);
    imshow("channel", channel);
    waitKey();
    return 0;
}

 

posted @ 2020-09-21 23:37  WeiAR  阅读(134)  评论(0编辑  收藏  举报