opencv三通道的iplimage怎么转为单通道?

狂奔的小乌龟啊 2016-03-10 01:48:13
网上很多都是首先把图片加载成单通道的,但是现在有三通道的iplimage数据结构,怎么转为单通道的数据结构?
...全文
762 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiter 2016-03-10
  • 打赏
  • 举报
回复





Converts an image from one color space to another. C++: void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 ) Python: cv2.cvtColor(src, code[, dst[, dstCn]]) → dst C: void cvCvtColor(const CvArr* src, CvArr* dst, int code) Python: cv.CvtColor(src, dst, code) → None Parameters: src – input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision floating-point. dst – output image of the same size and depth as src. code – color space conversion code (see the description below). dstCn – number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code . The function converts an input image from one color space to another. In case of a transformation to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on. The conventional ranges for R, G, and B channel values are: 0 to 255 for CV_8U images 0 to 65535 for CV_16U images 0 to 1 for CV_32F images In case of linear transformations, the range does not matter. But in case of a non-linear transformation, an input RGB image should be normalized to the proper value range to get the correct results, for example, for RGB \rightarrow L*u*v* transformation. For example, if you have a 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will have the 0..255 value range instead of 0..1 assumed by the function. So, before calling cvtColor , you need first to scale the image down: img *= 1./255; cvtColor(img, img, CV_BGR2Luv); If you use cvtColor with 8-bit images, the conversion will have some information lost. For many applications, this will not be noticeable but it is recommended to use 32-bit images in applications that need the full range of colors or that convert an image before an operation and then convert back. If conversion adds the alpha channel, its value will set to the maximum of corresponding channel range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F. The function can do the following transformations: RGB \leftrightarrow GRAY ( CV_BGR2GRAY, CV_RGB2GRAY, CV_GRAY2BGR, CV_GRAY2RGB ) Transformations within RGB space like adding/removing the alpha channel, reversing the channel order, conversion to/from 16-bit RGB color (R5:G6:B5 or R5:G5:B5), as well as conversion to/from grayscale using: \text{RGB[A] to Gray:} \quad Y \leftarrow 0.299 \cdot R + 0.587 \cdot G + 0.114 \cdot B and \text{Gray to RGB[A]:} \quad R \leftarrow Y, G \leftarrow Y, B \leftarrow Y, A \leftarrow \max (ChannelRange) The conversion from a RGB image to gray is done with: cvtColor(src, bwsrc, CV_RGB2GRAY); More advanced channel reordering can also be done with mixChannels() .
puddingli 2016-03-10
  • 打赏
  • 举报
回复
cvCvtColor(src,dst,CV_BGR2GRAY); //例如8位彩色图像转成灰度图像
  • 打赏
  • 举报
回复
解决了,问题出在qt中显示图像,Format::RGB888没改

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧