[OpenCV] Samples 16: Decompose and Analyse RGB channels
物体的颜色特征决定了灰度处理不是万能,对RGB分别处理具有相当的意义。
#include <iostream>
#include <stdio.h>
#include "cv.h"
#include <highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/flann/flann.hpp> using namespace std;
using namespace cv; #define PATH_IMG01 "../lolo.jpg" IplImage *g_pGrayImage = NULL;
IplImage *g_pGrayImg4ChannelR = NULL;
IplImage *g_pGrayImg4ChannelG = NULL;
IplImage *g_pGrayImg4ChannelB = NULL; IplImage *g_pBinaryImg4ChannelR = NULL;
IplImage *g_pBinaryImg4ChannelG = NULL;
IplImage *g_pBinaryImg4ChannelB = NULL;
IplImage *g_pBinaryImg4ChannelC = NULL; const char *pstrWindowsToolBarName4ChB = "ToolBarName4ChannelB";
const char *pstrWindowsToolBarName4ChG = "ToolBarName4ChannelG";
const char *pstrWindowsToolBarName4ChR = "ToolBarName4ChannelR";
const char *pstrWindowsToolBarName4ChC = "ToolBarName4ChannelC"; const char *pstrWindowsSrcTitle = "SrcImageTitle";
const char *pstrWindowsBinaryTitle4ChB = "BinaryTitle4B";
const char *pstrWindowsBinaryTitle4ChG = "BinaryTitle4G";
const char *pstrWindowsBinaryTitle4ChR = "BinaryTitle4R";
const char *pstrWindowsBinaryTitle4ChC = "BinaryTitle4C"; const char *pstr_title_chB = "Binary Image for Channel B";
const char *pstr_title_chG = "Binary Image for Channel G";
const char *pstr_title_chR = "Binary Image for Channel R"; void on_trackbar_channelB(int pos)
{
Mat src = Mat(g_pGrayImg4ChannelB);
Mat dst;
IplImage img_out; ////////////////////////////////////////////////////////
GaussianBlur(src,dst,Size(,),,);
// medianBlur(src,dst,10);
// blur(src,dst,Size(5,5),Point(-1,-1));
// bilateralFilter(src,dst,25, 25*2, 25/2);
img_out = IplImage(dst);
//////////////////////////////////////////////////////// // (1)
cvThreshold(&img_out, g_pBinaryImg4ChannelB, pos, , CV_THRESH_BINARY); // (2)
int gap = ;
IplImage *pBinaryImg4ChannelB = cvCreateImage(cvGetSize(g_pGrayImg4ChannelB), IPL_DEPTH_8U, );
cvThreshold(&img_out, pBinaryImg4ChannelB, pos + gap, , CV_THRESH_BINARY); // (3)
IplImage *pBinaryImg4ChannelB_DV = cvCreateImage(cvGetSize(g_pBinaryImg4ChannelB), IPL_DEPTH_8U, );
cvAbsDiff(g_pBinaryImg4ChannelB, pBinaryImg4ChannelB, pBinaryImg4ChannelB_DV);
// cvShowImage(pstrWindowsBinaryTitle4ChB, pBinaryImg4ChannelB_DV);
cvShowImage(pstrWindowsBinaryTitle4ChB, g_pBinaryImg4ChannelB);
} void on_trackbar_channelG(int pos)
{
Mat src = Mat(g_pGrayImg4ChannelG);
Mat dst;
IplImage img_out; ////////////////////////////////////////////////////////
GaussianBlur(src,dst,Size(,),,);
img_out = IplImage(dst);
//////////////////////////////////////////////////////// // (1)
cvThreshold(&img_out, g_pBinaryImg4ChannelG, pos, , CV_THRESH_BINARY); // (2)
int gap = ;
IplImage *pBinaryImg4ChannelG = cvCreateImage(cvGetSize(g_pGrayImg4ChannelG), IPL_DEPTH_8U, );
cvThreshold(&img_out, pBinaryImg4ChannelG, pos + gap, , CV_THRESH_BINARY); // (3)
IplImage *pBinaryImg4ChannelG_DV = cvCreateImage(cvGetSize(g_pBinaryImg4ChannelG), IPL_DEPTH_8U, );
cvAbsDiff(g_pBinaryImg4ChannelG, pBinaryImg4ChannelG, pBinaryImg4ChannelG_DV);
// cvShowImage(pstrWindowsBinaryTitle4ChG, pBinaryImg4ChannelG_DV);
cvShowImage(pstrWindowsBinaryTitle4ChG, g_pBinaryImg4ChannelG);
} void on_trackbar_channelR(int pos)
{
Mat src = Mat(g_pGrayImg4ChannelR);
Mat dst;
IplImage img_out; ////////////////////////////////////////////////////////
GaussianBlur(src,dst,Size(,),,);
img_out = IplImage(dst);
// cvShowImage( "Task 8*: Gaussian Blur", &img_out);
// cvWaitKey(0);
//////////////////////////////////////////////////////// // (1)
cvThreshold(&img_out, g_pBinaryImg4ChannelR, pos, , CV_THRESH_BINARY); // (2)
int gap = ;
IplImage *pBinaryImg4ChannelR = cvCreateImage(cvGetSize(g_pGrayImg4ChannelR), IPL_DEPTH_8U, );
cvThreshold(&img_out, pBinaryImg4ChannelR, pos + gap, , CV_THRESH_BINARY); // (3)
IplImage *pBinaryImg4ChannelR_DV = cvCreateImage(cvGetSize(g_pBinaryImg4ChannelR), IPL_DEPTH_8U, );
cvAbsDiff(g_pBinaryImg4ChannelR, pBinaryImg4ChannelR, pBinaryImg4ChannelR_DV);
// cvShowImage(pstrWindowsBinaryTitle4ChR, pBinaryImg4ChannelR_DV);
cvShowImage(pstrWindowsBinaryTitle4ChR, g_pBinaryImg4ChannelR);
} void on_trackbar_channelC(int pos)
{
cvOr(g_pBinaryImg4ChannelB, g_pBinaryImg4ChannelG, g_pBinaryImg4ChannelC);
cvOr(g_pBinaryImg4ChannelC, g_pBinaryImg4ChannelR, g_pBinaryImg4ChannelC); cvShowImage(pstrWindowsBinaryTitle4ChC, g_pBinaryImg4ChannelC);
} int main(void)
{
// 1. src image and resize.
Mat src = imread(PATH_IMG01);
const int zoom = ;
resize(src, src, Size(src.cols/zoom, src.rows/zoom)); IplImage srcImage = IplImage(src);
IplImage *pSrcImage = &srcImage; // 2. split r, g, b channel images.
Mat channel[]; split(pSrcImage, channel); // imshow("B",channel[0]);
// imshow("G",channel[1]);
// imshow("R",channel[2]);
// waitKey(0); IplImage img_channelB = IplImage(channel[]);
IplImage img_channelG = IplImage(channel[]);
IplImage img_channelR = IplImage(channel[]); g_pGrayImg4ChannelB = &img_channelB;
g_pGrayImg4ChannelG = &img_channelG;
g_pGrayImg4ChannelR = &img_channelR; // 3. get r, g, b binary images.
g_pBinaryImg4ChannelB = cvCreateImage(cvGetSize(g_pGrayImg4ChannelB), IPL_DEPTH_8U, );
g_pBinaryImg4ChannelG = cvCreateImage(cvGetSize(g_pGrayImg4ChannelG), IPL_DEPTH_8U, );
g_pBinaryImg4ChannelR = cvCreateImage(cvGetSize(g_pGrayImg4ChannelR), IPL_DEPTH_8U, ); // 4.1 show src image.
cvNamedWindow(pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE);
cvShowImage(pstrWindowsSrcTitle, pSrcImage); // 4.2 create r, g, b windows.
cvNamedWindow(pstrWindowsBinaryTitle4ChB, CV_WINDOW_AUTOSIZE);
cvNamedWindow(pstrWindowsBinaryTitle4ChG, CV_WINDOW_AUTOSIZE);
cvNamedWindow(pstrWindowsBinaryTitle4ChR, CV_WINDOW_AUTOSIZE); // 4.3 create toolbar for r, g, b windows.
int nThreshold = ;
cvCreateTrackbar(pstrWindowsToolBarName4ChB, pstrWindowsBinaryTitle4ChB, &nThreshold, , on_trackbar_channelB);
cvCreateTrackbar(pstrWindowsToolBarName4ChG, pstrWindowsBinaryTitle4ChG, &nThreshold, , on_trackbar_channelG);
cvCreateTrackbar(pstrWindowsToolBarName4ChR, pstrWindowsBinaryTitle4ChR, &nThreshold, , on_trackbar_channelR); // 4.4 create combine result show.
g_pBinaryImg4ChannelC = cvCreateImage(cvGetSize(g_pGrayImg4ChannelR), IPL_DEPTH_8U, );
cvNamedWindow(pstrWindowsBinaryTitle4ChC, CV_WINDOW_AUTOSIZE);
cvCreateTrackbar(pstrWindowsToolBarName4ChC, pstrWindowsBinaryTitle4ChC, &nThreshold, , on_trackbar_channelC); // 4.5 run.
on_trackbar_channelB();
on_trackbar_channelG();
on_trackbar_channelR();
on_trackbar_channelC(); cvWaitKey(); // 5. destroy trash.
cvDestroyWindow(pstrWindowsSrcTitle);
cvDestroyWindow(pstrWindowsBinaryTitle4ChB);
cvDestroyWindow(pstrWindowsBinaryTitle4ChG);
cvDestroyWindow(pstrWindowsBinaryTitle4ChR);
cvDestroyWindow(pstrWindowsBinaryTitle4ChC); cvReleaseImage(&pSrcImage);
cvReleaseImage(&g_pBinaryImg4ChannelB);
cvReleaseImage(&g_pBinaryImg4ChannelG);
cvReleaseImage(&g_pBinaryImg4ChannelR);
cvReleaseImage(&g_pBinaryImg4ChannelC); return ;
}
HSV channels 能更好地解决问题? 亮度60-80之间是一个不错的判定效果。
IplImage* pSrcHsv=cvCreateImage(cvGetSize(pSrcImage),IPL_DEPTH_8U,);
cvCvtColor(pSrcImage, pSrcHsv, CV_BGR2HSV); Mat channel[];
split(pSrcHsv, channel);
相关代码
通过亮度通道进行二值刷选后,再采用轮廓线判断继续缩小范围。
是否有判别基本几何形状的高效方法,找出其中的凸四边形?
Sol 01: “面积比”: size of contour/size of its bounding rectangle
[OpenCV] Samples 04: contours2
[OpenCV] Samples 05: convexhull
[OpenCV] Samples 16: Decompose and Analyse RGB channels的更多相关文章
- OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解
原文:OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解 Title : cv::Mat depth/dims/channels ...
- [OpenCV] Samples 10: imagelist_creator
yaml写法的简单例子.将 $ ./ 1 2 3 4 5 命令的参数(代表图片地址)写入yaml中. 写yaml文件. 参考:[OpenCV] Samples 06: [ML] logistic re ...
- JS-011-颜色进制转换(RGB转16进制;16进制转RGB)
在网页开发的时候,经常需要进行颜色设置,因而经常需要遇到进行颜色进制转换的问题,例如:RGB转16进制:16进制转RGB),前几天在测试的时候,发现网站的颜色进制转换某类16进制颜色(例如:#0000 ...
- js 颜色16进制转RGB方法
//颜色16进制转RGB方法 String.prototype.colorRgb = function(){ var sColor = this.toLowerCase(); //十六进制颜色值的正则 ...
- bgcolor RGB 和16进制之间的转换,16进制转RGB,源码
<p>bgcolor RGB 和16进制之间的转换,16进制转RGB,源码例如:<br /> 输入 201,255,201 转换成 #C9FFC9</p> < ...
- JS实现16进制和RGB转换
作为前端开发而言,不可避免的会遇到颜色取值,字符串和数字直接的转换,博主为此写了一个小工具,实现色值之间的在线转换. 前置知识点: parseInt, toString parseInt(value ...
- OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解 (转)
cv::Matdepth/dims/channels/step/data/elemSizeThe class Mat represents an n-dimensional dense numeric ...
- 访问图像中的像素[OpenCV 笔记16]
再更一发好久没更过的OpenCV,不过其实写到这个部分对计算机视觉算法有所了解的应该可以做到用什么查什么了,所以后面可能会更的慢一点吧,既然开了新坑,还是机器学习更有研究价值吧... 图像在内存中的存 ...
- OpenCV 学习笔记(9)RGB转换成灰度图像的一个常用公式Gray = R*0.299 + G*0.587 + B*0.114
https://blog.csdn.net/fly_wt/article/details/86432886 RGB转换成灰度图像的一个常用公式是:Gray = R*0.299 + G*0.587 + ...
随机推荐
- 初识CocosCreator的一些问题
本文的cocos creator版本为v1.9.0,除此外大部分都是以v1.9.3版本 1.color赋值 cc.Label组件并没有颜色相关的属性,但是Node有color的属性. //如果4个参数 ...
- Java svnkit check update commit
import java.io.File; import org.apache.log4j.Logger;import org.tmatesoft.svn.core.SVNCommitInfo;impo ...
- POIUtil
package com.util.poi; import java.io.ByteArrayOutputStream; import java.io.IOException; import java. ...
- POI HSSFCellStyle 设置 Excel 单元格样式
POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...
- 如何在Angular优雅编写HTTP请求
原文:https://segmentfault.com/a/1190000010570799 ----------------------------------------------------- ...
- SqlSession介绍
SqlSession是MyBatis的关键对象,是执行持久化操作的对象,类似于JDBC中的Connection.它是应用程序与持久存储层之间执行交互操作的一个单线程对象,也是MyBatis执行持久化操 ...
- Apache Kafka学习 (二) - 多代理(broker)集群
1. 配置server.properties > cp config/server.properties config/server-1.properties> cp config/ser ...
- 微软BI 之SSRS 系列 - 基于时间段参数的 MDX 查询以及时间日历 Date Picker 的时间类型参数化
今天在天善问答里看到一个问题,如果我没有理解错的话,它应该是指比如在一个报表中选取一个时间段,然后求出这个时间段的某个 Measure 的 SUM 和.并且同时求出这两个时间点对应的上一年的时间点之间 ...
- EasyUI 的 combotree 加载数据后折叠起来,并且只允许单击子节点的写法
$(source).combotree({ url: '', width: kuan, valueField: 'id', textField: 'text', onlyLeafCheck: true ...
- golang ----gc问题
go程序内存占用大的问题 这个问题在我们对后台服务进行压力测试时发现,我们模拟大量的用户请求访问后台服务,这时各服务模块能观察到明显的内存占用上升.但是当停止压测时,内存占用并未发生明显的下降.花了很 ...