【OpenCV 基础知识 4】分离图像通道

cvSplit()是openCV中的一个函数,它分别复制每个通道到多个单通道图像。
void cvSplit( const CvArr* src, CvArr* dst0, CvArr* dst1, CvArr* dst2, CvArr* dst3 );.cvSplit()函数将复制src的各个通道到图像dst0,dst1,dst2和dst3中。如果源图像少于4个通道的情况下,那么传递给cvSplit()的不必要的目标参数可设置为NULL。

cvSum函数
其结构
CvScalar cvSum(//计算arr各通道所有像素总和 CvArr* arr//目标矩阵 );

program cv_Sum;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  ocv.highgui_c,
  ocv.core_c,
  ocv.core.types_c,
  ocv.imgproc_c,
  ocv.imgproc.types_c,
  uResourcePaths;

const
  filename = cResourceMedia + 'cat2.jpg';

var
  img: pIplImage;
  channels: array [0 .. 2] of pIplImage;
  BGRSum: Array [0 .. 2] of TCvScalar;
  i, total: Integer;

begin
  try

    img := cvLoadImage(filename);

    for i := 0 to 2 do
      channels[i] := cvCreateImage(cvGetSize(img), 8, 1);

    cvSplit(img, channels[0], channels[1], channels[2]);

    for i := 0 to 2 do
      BGRSum[i] := cvSum(channels[i]);

    total := img^.width * img^.height * 255;

    WriteLn('Color percentage of RGB(ex red 50%, green 25% and blue %25) in an image is');

    WriteLn('red:   ', BGRSum[2].val[0] / total * 100:2:2);
    WriteLn('green: ', BGRSum[1].val[0] / total * 100:2:2);
    WriteLn('blue:  ', BGRSum[0].val[0] / total * 100:2:2);

    cvNamedWindow('channels0');
    cvShowImage('channels0', channels[0]);
    cvNamedWindow('channels1');
    cvShowImage('channels1', channels[1]);
    cvNamedWindow('channels2');
    cvShowImage('channels2', channels[2]);
    cvWaitKey();
    readln;
  except
    on E: Exception do
      WriteLn(E.ClassName, ': ', E.Message);
  end;

end.
  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

D-Nolan

请我喝杯咖啡吧,鼓励一下创作!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值