OPENCV入门之启动摄像头《分水岭算法》

首先这代码是改编过来,原本是不能自启的,下面直接上代码吧,基础的东西还是要机子动手多打代码,多理解,虽然我是刚入门的,如果有错误或者有更好的想法大家一块交流,谢谢。

 
#include "opencv2/imgproc/imgproc.hpp"  
#include "opencv2/highgui/highgui.hpp"  

 

#include <iostream>  
  
using namespace cv;  
using namespace std;  


  
Vec3b RandomColor(int value);  //生成随机颜色函数  
  
int main( int argc, char* argv[] )  
{
system("color 1E");
Mat frame;
        Mat edges;
    VideoCapture cap(0);  
    if(!cap.isOpened())  
    {  
        return -1;  
    }  
 Mat srcImage,grayImage;
    bool stop = false;  
    while(!stop)  
    {  
        cap>>frame;  
cvtColor(frame,edges,CV_RGB2GRAY);
GaussianBlur(edges,edges,Size(5,5),2);
imshow("GaussianBlur",edges);
Canny(edges,edges,80,150);
imshow("Canny",edges);
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(edges,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());
Mat imageContours=Mat::zeros(frame.size(),CV_8UC1);
Mat marks(frame.size(),CV_32S);
marks=Scalar::all(0);
int index=0;
int compCount = 0;
for( ; index >= 0; index = hierarchy[index][0], compCount++ )   
    {  
        //对marks进行标记,对不同区域的轮廓进行编号,相当于设置注水点,有多少轮廓,就有多少注水点  
        drawContours(marks, contours, index, Scalar::all(compCount+1), 1, 8, hierarchy);  
        drawContours(imageContours,contours,index,Scalar(255),1,8,hierarchy);    
    }
        Mat marksShows;
convertScaleAbs(marks,marksShows);
imshow("marksShows",marksShows);
imshow("轮廓",imageContours);
watershed(frame,marks);

Mat afterWatershed;
convertScaleAbs(marks,afterWatershed);
imshow("After",afterWatershed);
Mat PerspectiveImage=Mat::zeros(frame.size(),CV_8UC3);
for(int i=0;i<marks.rows;i++)
{
for(int j=0;j<marks.rows;j++)
{
int index=marks.at<int>(i,j);
if(marks.at<int>(i,j)==-1)
{
PerspectiveImage.at<Vec3b>(i,j)=Vec3b(255,255,255);
}
else
{
PerspectiveImage.at<Vec3b>(i,j)=RandomColor(index);
}
}
}
     imshow("colorfill", PerspectiveImage);
Mat wshed;
addWeighted(frame,0.4, PerspectiveImage,0.6,0,wshed);
imshow("frame",wshed);
waitKey();
}
 }
Vec3b RandomColor(int value)
{
value=value%255;
RNG rng;
int aa=rng.uniform(0,value);
int bb=rng.uniform(0,value);
int cc=rng.uniform(0,value);
return Vec3b(aa,bb,cc);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值