代码拉取完成,页面将自动刷新
// CvTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
void Resize(Mat &img,float ratio)
{
resize(img,img,Size(img.cols*ratio,img.rows*ratio));
}
int main13()
{
Mat image = imread("D:\\00004.jpg");
Mat hsvImg;
cvtColor(image, hsvImg, CV_BGR2HSV);
enum colorType{Red = 0, Green, Blue, ColorButt};
const Scalar hsvRedLo( 0, 40, 40);
const Scalar hsvRedHi(40, 255, 255);
const Scalar hsvGreenLo(41, 40, 40);
const Scalar hsvGreenHi(90, 255, 255);
const Scalar hsvBlueLo(100, 40, 40);
const Scalar hsvBlueHi(140, 255, 255);
vector<Scalar> hsvLo;
hsvLo.push_back(hsvRedLo);
hsvLo.push_back(hsvGreenLo);
hsvLo.push_back(hsvBlueLo);
vector<Scalar> hsvHi;
hsvHi.push_back(hsvRedHi);
hsvHi.push_back(hsvGreenHi);
hsvHi.push_back(hsvBlueHi);
// 查找指定范围内的颜色
Mat imgThresholded;
int colorIdx = 0;
inRange(hsvImg, hsvLo[colorIdx], hsvHi[colorIdx], hsvImg);
// 转换成二值图
//threshold(imgThresholded, imgThresholded, 1, 255, THRESH_BINARY);
//imwrite("D:\\res.jpg",imgThresholded);
Mat res;
cvtColor(hsvImg,res,CV_HSV2BGR);
imwrite("D:\\res.jpg",res);
return 0;
}
void GetRed();
int PowerTrans(Mat & pSrc,double r,double b=0, double c=1)
{
//y=cxr+b
// 映射表,用于256种灰度变换后的值
BYTE map[256];
// 保存运算后的临时值
double dTemp;
for (int i = 0; i < 256; i++)
{
// 计算当前像素变换后的值
dTemp = c * pow(i / 255.0, r) * 255 + b;
// 如果超界则修改其值
if (dTemp < 0)
dTemp = 0.0;
else if (dTemp > 255)
dTemp = 255;
// 四舍五入
map[i] = int(dTemp + 0.5);
}
int columns=pSrc.cols;
int channels = pSrc.channels();
int count = 0;
for(int i=0;i<pSrc.rows;i++)
{
uchar* p=pSrc.ptr(i);//获取Mat某一行的首地址
for(int j=0;j<columns*channels;j++)
{
*(p+j) = map[*(p+j)];
}
}
return 0;
}
int GetRedProcess(string path)
{
int iLowM = 0;
int iHighM = 10;
int iLowH = 140;
int iHighH = 180;
int iLowS = 20;
int iHighS = 255;
int iLowV = 6;
int iHighV = 255;
Mat imgOriginal = imread(path,CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); // read a new frame from video
//工作特殊需求
if(imgOriginal.rows <3000 && imgOriginal.cols < 3000)
PowerTrans(imgOriginal,2);//幂次变换使图像变暗一些
//
Mat imgHSV;
vector<Mat> hsvSplit;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
//CvScalar scalarA = cvAvg(&IplImage(imgHSV));
//printf ("avg A:%f %f %f\n", scalarA.val[0], scalarA.val[1], scalarA.val[2]);
//因为我们读取的是彩色图,直方图均衡化需要在HSV空间做
split(imgHSV, hsvSplit);
equalizeHist(hsvSplit[2],hsvSplit[2]);
merge(hsvSplit,imgHSV);
Mat imgThresholded,imgThresholded2;
//imwrite("D:\\hsv1.jpg",imgHSV);
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
inRange(imgHSV, Scalar(iLowM, iLowS, iLowV), Scalar(iHighM, iHighS, iHighV), imgThresholded2); //Threshold the image
imgThresholded += imgThresholded2;
threshold(imgThresholded, imgThresholded, 128, 255, CV_THRESH_BINARY);
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element);
morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);
//imwrite("D:\\res.jpg",imgThresholded);
Resize(imgThresholded,0.4);
imshow("1",imgThresholded);
waitKey(0);
//threshold(imgThresholded, m_matColor2Gray, 128, 255, CV_THRESH_BINARY);
//vector<vector<cv::Point>> imgContours;
//vector<Vec4i> imgHierarchy; // 分层表示
//findContours(m_matColor2Gray.clone(), imgContours, imgHierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
//Mat dst= Mat::zeros(m_matColor2Gray.size(), CV_8UC3);
//drawContours(dst,imgContours,-1,Scalar(0,0,255),1,8);
/*for (int i = 0; i < imgContours.size(); i++)
{
cv::Rect rect = boundingRect(imgContours[i]);
rectangle(dst,rect,Scalar(0,0,255));
cout<<rect.height<<" ";
}
imwrite("D:\\dst.jpg",dst);*/
return 0;
}
int GetYCrCb(Mat &img)
{
Mat Y,Cr,Cb;
Mat result; //拷贝备份
vector<Mat> channels;
/*转换颜色空间并分割颜色通道*/
cvtColor(img, img, CV_BGR2YCrCb);
split(img, channels);
Cr = channels.at(1);
Cb = channels.at(2);
threshold(Cr,result,135,255,THRESH_BINARY);
/*for(int j = 0; j < Cb.rows; j++)
{
uchar* currentCb = Cb.ptr< uchar>(j);
uchar* current = result.ptr< uchar>(j);
for (int i = 0; i < Cb.cols; i++)
{
if(currentCb[i] < 90)
{
current[i] = 0;
}
}
}*/
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(result, result, MORPH_OPEN, element);
//Resize(frame,0.4);
Resize(result,0.4);
//imshow("frame", frame);
imshow("result", result);
waitKey(0);
return 0;
}
int main()
{
for(int i = 10; i < 11; i++)
{
stringstream ss;
if(i < 10)
{
ss <<"D:\\pdf2jpg\\red\\0000"<<i<<".jpg";
}
else if(i >= 10)
{
ss <<"D:\\pdf2jpg\\red\\000"<<i<<".jpg";
}
GetRedProcess(ss.str());
}
return 0;
}
int GetWhitePixels(Mat &src)
{
if(src.channels() > 1) return 0;
int rows=src.rows;
int columns=src.cols;
int count = 0;
for(int i=0;i<rows;i++)
{
uchar* p=src.ptr(i);//获取Mat某一行的首地址
for(int j=0;j<columns;j++)
{
if(*(p+j) >190)
count++;
}
}
return count;
}
void GetRed()
{
//freopen("D:\\1.txt","w",stdout);
Mat img = imread("D:\\src.jpg");//读入图像
cout<<img.channels()<<img.dims<<endl;
//threshold(img, img, 200, 255, CV_THRESH_BINARY);
Mat hsv;// = Mat(img.rows,img.cols,CV_32F);
cvtColor(img,hsv,CV_BGR2HSV);
//imshow("hsv",hsv);
int rows=hsv.rows;
int columns=hsv.cols;
for(int i=0;i<rows;i++)
{
uchar* p=hsv.ptr(i);//获取Mat某一行的首地址
for(int j=0;j<columns;j++)
{
int q=*(p+j*3);//HSV间隔排列,现取H的地址
if (!(((q>0)&&(q<8)) || (q>128)&&(q<180)))
{
//*(p+j*3)=0;
*(p+j*3+1)=0;
*(p+j*3+2)=0;
}
else
{ // if(*(p+j*3+1) <=80) continue;
if(*(p+j*3+2) <=50 || *(p+j*3+2) >220)
{
//if(*(p+j*3+1) <=80)
// *(p+j*3+2)=0;
continue;
}
//printf("%d ",*(p+j*3+2));
*(p+j*3+1)=255;
*(p+j*3+2)=255;
}
}
}
imwrite("D:\\hsv.jpg",hsv);
Mat res;
cvtColor(hsv,res,CV_HSV2BGR);
imwrite("D:\\res.jpg",res);
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
//腐蚀操作
//dilate(res,res,element);
erode(res,res,element);
//膨胀操作
dilate(res,res,element,Point(-1,-1),1);
//erode(res,res,element);
//Mat res;
threshold(img, res, 200, 255, CV_THRESH_BINARY);
imwrite("D:\\2.jpg",res);
std::vector<Mat> channels; //BGR
split(res, channels);
//imshow("B",channels[0]);
//imshow("G",channels[1]);
imwrite("D:\\R.jpg",channels[2]);
//imshow("4",channels[2]-channels[1]);
//waitKey(0);
//dilate(res,res,element,Point(-1,-1),1);
//threshold(res, res, 128, 255, CV_THRESH_BINARY);
Mat red = channels[2]-(channels[1]+channels[0]);
cout<<" "<<GetWhitePixels(red)<<endl;
imwrite("D:\\res4.jpg",red); //去白点
return;
//vector<vector<cv::Point>> imgContours;
//vector<Vec4i> imgHierarchy; // 分层表示
//findContours(red.clone(), imgContours, imgHierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
//Mat dst(red.size(), CV_8UC1,Scalar(255));
/*for (int i = 0; i < imgContours.size(); i++)
{
Scalar color = Scalar(255,255,255);
//随机颜色绘制轮廓
drawContours(dst, imgContours, i, color, 2, 8, imgHierarchy, 0, Point());
} */
//for (int i = 0; i < imgContours.size(); i++)
{
//cv::RotatedRect rect = fitEllipse(Mat(imgContours));
//rectangle(dst,rect.boundingRect(),Scalar(0),1,8,0);
//ellipse(dst,rect,Scalar(0));
}
//drawContours(dst, imgContours, -1, Scalar(0), 1);
Mat contours;
Canny(red,contours,125,350);
std::vector<Vec2f>lines;
HoughLines(contours,lines,1,3.1415/180,40);
imwrite("D:\\dst1.jpg",contours);
}
//该方法可能产生误检点,需要使用形态学后处理
void GetRedComponet(Mat &srcImg)
{
//如果直接对srcImg处理会改变main()函数中的实参
Mat dstImg = srcImg.clone();
Mat_<Vec3b>::iterator it = dstImg.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = dstImg.end<Vec3b>();
for(; it != itend; it++)
{
if((*it)[2] > 190)//对红色分量做阈值处理
{
(*it)[0] = 0;
(*it)[1] = 0;
//(*it)[2] = 255;//红色分量保持不变
}
else
{
(*it)[0] = 0;
(*it)[1] = 0;
(*it)[2] = 0;
}
}
srcImg = dstImg;
//imshow("红色分量图 by 阈值法", dstImg);
//waitKey(0);
}
//将整幅图中的红色分量都提取出来
void GetRedComponetBySplit(Mat &srcImg)
{
Mat imgROI;
vector<Mat>channels;
split(srcImg, channels);
Mat blueComponet = channels.at(0);
Mat greenComponet = channels.at(1) ;
blueComponet = Mat::zeros(srcImg.size(), CV_8UC1);//Mat相当于指针,会对chnnels.at(0)重新赋值
greenComponet = Mat::zeros(srcImg.size(), CV_8UC1);
merge(channels, imgROI);//仅仅保留红色分量,其他分量赋值为0
srcImg = imgROI;
//imshow("红色分量图 by split()函数", imgROI);
//waitKey(0);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。