【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//目标矩阵 );

js 复制代码
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.
相关推荐
我自纵横202332 分钟前
事件处理程序
开发语言·前端·javascript·css·json·ecmascript
坊钰36 分钟前
【MySQL 数据库】数据类型
java·开发语言·前端·数据库·学习·mysql·html
我是小路路呀1 小时前
css 文字换行每一个字渐变
前端·css
谢小飞1 小时前
Threejs全球坐标分布效果
前端·three.js
喝拿铁写前端1 小时前
🚀从 0 到 1 构建字段推荐引擎:20+ 工具方法一文打尽!
前端
森叶1 小时前
免费Deepseek-v3接口实现Browser-Use Web UI:浏览器自动化本地模拟抓取数据实录
前端·人工智能·自动化
拉不动的猪1 小时前
刷刷题50(vue3)
前端·javascript·面试
胡八一2 小时前
使用 Less 实现 PC 和移动端样式适配
前端·css·less
加减法原则2 小时前
字节面试题之如何取消一个正在发送的请求
前端
ZSK62 小时前
【HTML】分享一个自己写的3*3拼图小游戏
前端·javascript·html