首页 技术 正文
技术 2022年11月23日
0 收藏 430 点赞 4,583 浏览 6751 个字

物体的颜色特征决定了灰度处理不是万能,对RGB分别处理具有相当的意义。

[OpenCV] Samples 16: Decompose and Analyse RGB channels

 #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之间是一个不错的判定效果。

[OpenCV] Samples 16: Decompose and Analyse RGB channels[OpenCV] Samples 16: Decompose and Analyse RGB channels

IplImage* pSrcHsv=cvCreateImage(cvGetSize(pSrcImage),IPL_DEPTH_8U,);
cvCvtColor(pSrcImage, pSrcHsv, CV_BGR2HSV);Mat channel[];
split(pSrcHsv, channel);

相关代码

通过亮度通道进行二值刷选后,再采用轮廓线判断继续缩小范围。

[OpenCV] Samples 16: Decompose and Analyse RGB channels

是否有判别基本几何形状的高效方法,找出其中的凸四边形?

Sol 01: “面积比”: size of contour/size of its bounding rectangle

[OpenCV] Samples 04: contours2

[OpenCV] Samples 05: convexhull

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,254
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,692
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,531
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,302
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,942
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,102