首页 > 解决方案 > How do I get the region of interest in 3 channels when capturing images with computer vision?

问题描述

I am learning about computer vision. I get ROI in mono channel like this.(cf. I calculate sample average pixel color.)

std::string path = "C:\\image\\Lenna.png";
cv::Mat mImage = cv::imread(path);
cv::Mat mImage_mono;
cv::cvtColor(mImage, mImage_mono, CV_RGB2GRAY);
int width = mImage_mono.cols;
int height = mImage_mono.rows;
unsigend char * PImage = mImage_mono.data
const int kernel_size = 100;
const int kernel_size_half = 100/2;
int sum
int avg
sum = 0;

for (int row = height / 2 - kernel_size_half; row < height / 2 + kernel_size_half; row++) {
        for (int col = width / 2 - kernel_size_half; col < width / 2 + kernel_size_half; col++)
        {
            int index = row * width + col;
            sum+= pImage[index];
        }
    }
    avg = sum / (kernel_size * kernel_size);

I want to get ROI in 3 channels(R,G,B) like mono channels(I want using 'for' code) and I want something like face detection only in ROI. In 3 channels, I have to take into account about array. I know that array takes data ordering (B, G, R). So I think I have to multiply 3 at width and height and i do that but do not work correctly. how I can get ROI in multi channels not using fuction "cv::cvSetImageROI"?

标签: c++opencvcomputer-vision

解决方案


你想通过颜色通道分割图像并获得每个通道的ROI吗?

cv::Rect r(0, 0, roi_width, roi_height);
cv::Mat bgr[3];
cv::Mat b_roi;
cv::split(image, bgr);
b_roi = bgr[0](r);  // get the ROI of blue channel

希望这可以帮助


推荐阅读