使用opencV从RGB图像中提取一通道图像

发布于 2024-12-29 06:38:27 字数 95 浏览 0 评论 0原文

我正在使用 QT 和 OpenCV,我有一个需要提取的正方形,但我需要使用从 RGB 到一个通道(基本上是红色)的转换。我们非常欢迎任何建议,请随时建议使用哪些功能。提前致谢。

I'm working with QT and OpenCV, I have this square that I need to extract but I need to use conversion from RGB to one channel (RED basically). Any advice will be more than welcome, please feel free to advice which functions to use. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过期情话 2025-01-05 06:38:27

另一种方法是使用 extractChannel

cv::Mat channel;
//image is already loaded
cv::extractChannel(image, channel, 2);

这将提取来自图像的第三个通道并将结果保存在通道中。也可以使用 extractImageCOImixChannels 提取特定通道。

Another way is to use extractChannel:

cv::Mat channel;
//image is already loaded
cv::extractChannel(image, channel, 2);

This will extract the 3rd channel from image and save the result in channel. A particular channel can be extracted as well with extractImageCOI and mixChannels .

恬淡成诗 2025-01-05 06:38:27

我认为 cvSplit 就是您正在寻找的(文档)。例如,您可以使用它将 RGB 拆分为 R、G 和 B:

/* assuming src is your source image */
CvSize s = cvSize(src->width, src->height);
int d = src->depth;
IplImage* R = cvCreateImage(s, d, 1);
IplImage* G = cvCreateImage(s, d, 1);
IplImage* B = cvCreateImage(s, d, 1);
cvSplit(src, R, G, B, null);

请注意,您需要注意顺序;确保原始图像实际上按 R、G、B 顺序排列(很有可能是 B、G、R)。

I think cvSplit is what you're looking for (docs). You can use it, for example, to split RGB into R, G, and B:

/* assuming src is your source image */
CvSize s = cvSize(src->width, src->height);
int d = src->depth;
IplImage* R = cvCreateImage(s, d, 1);
IplImage* G = cvCreateImage(s, d, 1);
IplImage* B = cvCreateImage(s, d, 1);
cvSplit(src, R, G, B, null);

Note you'll need to be careful about the ordering; make sure that the original image is actually ordered as R, G, B (there's a decent chance it's B, G, R).

梦里人 2025-01-05 06:38:27

由于它被标记为 qt 我将给出一个 C++ 答案。

    // Create Windows
    namedWindow("Red",1);
    namedWindow("Green",1);
    namedWindow("Blue",1);

    // Create Matrices (make sure there is an image in input!)
    Mat input;
    Mat channel[3];

    // The actual splitting.
    split(input, channel);

    // Display
    imshow("Blue", channel[0]);
    imshow("Green", channel[1]);
    imshow("Red", channel[2]);

在 OpenCV 2.4.5 上测试

Since this is tagged qt I'll give a C++ answer.

    // Create Windows
    namedWindow("Red",1);
    namedWindow("Green",1);
    namedWindow("Blue",1);

    // Create Matrices (make sure there is an image in input!)
    Mat input;
    Mat channel[3];

    // The actual splitting.
    split(input, channel);

    // Display
    imshow("Blue", channel[0]);
    imshow("Green", channel[1]);
    imshow("Red", channel[2]);

Tested on OpenCV 2.4.5

何以心动 2025-01-05 06:38:27

据我所知,

cvtColor(src, bwsrc, CV_RGB2GRAY);

可以调用,其中 src 是多通道源图像,第三个参数表示目标中的通道数。因此,您可以在 OpenCV 中执行此操作并在 Qt 界面上显示图像。

另一方面,您可以使用适当的 split() 方法将通道拆分为单独的单通道数组。

http://opencv.willowgarage.com/documentation/cpp/core_operations_on_arrays.html#split

As far as I know a call to,

cvtColor(src, bwsrc, CV_RGB2GRAY);

Can do that, where src is the multi channel source image and the third parameter represents the number of channels in the destination. So, you can do that in OpenCV and display the image on your Qt interface.

On the other hand, you can split the channels into separate single channel arrays using appropriate split() method.

http://opencv.willowgarage.com/documentation/cpp/core_operations_on_arrays.html#split

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文