物体的颜色特征决定了灰度处理不是万能,对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