#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
#include "list.h"
#include "after.h"
using namespace cv;
using namespace std;
#define WINDOW_WIDTH 1000
#define WINDOW_NAME "绘制图形"
extern int pixel_num;
int main()
{
//第零步:变量定义
//容器
vector<vector<Point>>contours;
vector<vector<Point>>contours1;
vector<Vec4i>hierarchy;
Mat Image,m;
Mat grayImage; //定义灰度图
Mat Binary_image; //定义二值图
Mat GS_dst;
//摄像头
VideoCapture capture(0);
Mat frame;
//缩小图片
Mat Outputdst;
//调用摄像头
/*
while(1)
{
if(capture.read(frame)) //注意:摄像头显示图片的时候,必须先使用下这个if进行判断是否读取到了帧,否则极有可能出错
{
cout<<"摄像头打开正常,读到了图片"<<endl;
capture >> frame;
imshow("1",frame);
waitKey(67);
}
}
*/
// 第一步:CCD商标原图显示
Mat img = imread("CCD1.jpg",1); //载入图像
//namedWindow("CCD商标原图",WINDOW_NORMAL); //创建一个显示原图窗口
//imshow("CCD商标原图",img); //显示图像
// 第二步: 进行平滑处理 --- 高斯平滑
GaussianBlur(img,GS_dst,Size(5,5),0,0);
//namedWindow("CCD商标高斯平滑图",WINDOW_NORMAL); //创建一个显示高斯平滑图窗口
//imshow("CCD商标高斯平滑图",GS_dst); //显示图像
// 第三步: 进行阈值处理 --- 获取二值图
int val = 180; //阈值取值
cvtColor( GS_dst, grayImage, CV_RGB2GRAY ); //获得灰度图
//namedWindow("CCD商标灰度图",WINDOW_NORMAL); //创建一个显示灰度图窗口
//imshow("CCD商标灰度图",grayImage); //显示图像
threshold(grayImage,Binary_image,val,255,1);
//namedWindow("CCD商标二值图",WINDOW_NORMAL); //创建一个显示二值图窗口
//imshow("CCD商标二值图",Binary_image); //显示图像
//imwrite("best binary_image.jpg",Binary_image);
//第四步: 求最大外轮廓
findContours(Binary_image,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE);
Mat dstImage=Mat::zeros(Binary_image.rows,Binary_image.cols,CV_8UC1);
//namedWindow("CCD商标轮廓图",WINDOW_NORMAL); //创建一个窗口
for (int index=0;index>=0;index=hierarchy[index][0])
{
Scalar color(255,255,255);
drawContours(dstImage,contours,index,color,1,8,hierarchy);
//imshow("CCD商标轮廓图",dstImage);
}
imwrite("轮廓dstImage.jpg",dstImage);
//第五步: 一副图像的轮廓像素比较多,我们按比例进行缩放,降低数据处理的量:resize()函数
//方法一:
//Outputdst = Mat::zeros(512,512,CV_8UC3);
//resize(dstImage,Outputdst,Outputdst.size());
//方法二:
resize(dstImage,Outputdst,Size(),0.5,0.5);
//namedWindow("resize()之后的轮廓图");
//imshow("resize()之后的轮廓图",Outputdst);
//第六步: 遍历轮廓像素点
int num = 0;
findContours(Outputdst,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE);
for (int i=0;i<int(contours.size());i++)
{
for (int j=0;j<int(contours[i].size());j++)
{
ofstream f;
f.open("before.h",ios::out|ios::app);
f<<"points[0]"<<"["<<num++<<"]=Point("<<contours[i][j].x<<","<<contours[i][j].y<<");"<<endl;
}
}
//第七步: 将上面的轮廓抽出不必要的像素点
findContours(Outputdst,contours1,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE);
num = 0;
//创建头结点
extern struct node *pHeader;
pHeader = create_node(Point(0,0));
for (int i=0;i<int(contours1.size());i=i+25)
{
for (int j=0;j<int(contours1[i].size());j=j+25)
{
ofstream f;
f.open("after2.h",ios::out|ios::app);
f<<"points"<<"["<<num++<<"]=Point("<<contours1[i][j].x<<","<<contours1[i][j].y<<");"<<endl;
//f<<contours1[i][j].x<<","<<contours1[i][j].y<<endl;
//cout<<contours1[i][j].x<<","<<contours1[i][j].y<<endl;
//第九步:使用链表(堆内存)来存储像素点
insert_tail(pHeader, create_node(Point(contours1[i][j].x,contours1[i][j].y)));
}
}
//遍历链表的节点
pixel_num = bianli(pHeader); //使用val来接收遍历的返回值,即实际存储的像素的个数
printf("主程序中的pixel_num = %d\n",pixel_num);
//第八步:将剩下的点连起来
Mat atomImage(WINDOW_WIDTH,WINDOW_WIDTH,CV_8UC3,Scalar(255,255,255));//创建空白的Mat对象
//画多边形
DrawPolygon(atomImage);
//显示图形
namedWindow("WINDOW_NAME",WINDOW_NORMAL);
imshow("WINDOW_NAME",atomImage);
//图形写入文件
imwrite("1.jpg", atomImage);
waitKey(0);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
9:解决像素点生成后,需要手动操作的问题.zip_opencv 抽取像素点
共48个文件
tlog:19个
jpg:5个
h:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 101 浏览量
2022-09-21
21:51:02
上传
评论
收藏 5.38MB ZIP 举报
温馨提示
对一副图像进行预处理,然后提取轮廓,再将轮廓进行像素点抽取。
资源推荐
资源详情
资源评论
收起资源包目录
9:解决像素点生成后,需要手动操作的问题.zip (48个子文件)
9:解决像素点生成后,需要手动操作的问题
test.cpp 4KB
1.jpg 27KB
test.suo 20KB
test.sdf 13.52MB
before.h 171KB
test.vcxproj 5KB
test.vcxproj.filters 1KB
CCD1 - 副本.jpg 13KB
CCD1.jpg 30KB
after.h 601B
CCD.jpg 20KB
轮廓dstImage.jpg 28KB
list.h 1KB
after2.h 17KB
Debug
test.obj 356KB
test.log 4KB
vc100.idb 451KB
CL.write.1.tlog 264B
CL.read.1.tlog 9KB
link.5924.read.1.tlog 2B
mt.read.1.tlog 258B
test.exe.embed.manifest.res 472B
rc.write.1.tlog 238B
link.5924.write.1.tlog 2B
rc.read.1.tlog 230B
test.exe.embed.manifest 406B
mt.command.1.tlog 342B
test_manifest.rc 198B
cl.command.1.tlog 602B
link-cvtres.read.1.tlog 2B
test.ilk 767KB
test.exe 110KB
test.lastbuildstate 94B
link.5924-cvtres.read.1.tlog 2B
link.write.1.tlog 624B
test.exe.intermediate.manifest 381B
link-cvtres.write.1.tlog 2B
link.command.1.tlog 2KB
rc.command.1.tlog 450B
link.read.1.tlog 4KB
mt.write.1.tlog 258B
test.write.1.tlog 0B
link.5924-cvtres.write.1.tlog 2B
test.Build.CppClean.log 3KB
vc100.pdb 972KB
test.pdb 1.5MB
test.vcxproj.user 143B
test.sln 874B
共 48 条
- 1
资源评论
寒泊
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功