我是靠谱客的博主 感动酸奶,这篇文章主要介绍Matlab手工拖动鼠标绘制矩形和圆形并产生GroundTruth,现在分享给大家,希望可以做个参考。

需要人工合成一个数据集,在指定的图像上绘制圆形和矩形的“缺陷”,并根据绘制的缺陷产生相应的ground truth.为了一定程度上模拟真实的缺陷图像数据,我们在合成的缺陷图像上添加了椒盐噪声和高斯噪声。程序可以指定需要绘制的矩形和圆形的个数,Matlab代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
%用来产生合成缺陷数据集 clc; clear; imgDir='./Reference/'; resDir1='./SyntheticDefect/'; resDir2='./SyntheticGT/'; mkdir(resDir1); mkdir(resDir2); D=dir(imgDir); imgType=D(3).name(end-2:end); D=dir([imgDir '*.',imgType]); numImg=numel(D); for imgIndex=1:numImg imgFile=strcat(imgDir,D(imgIndex).name); img=double(rgb2gray(imread(imgFile))); [m,n]=size(img); recGT=zeros(m,n); cirGT=zeros(m,n); synDefect=img; recPrompt = 'Input the number of rectangles:'; recNum = input(recPrompt);%据传input只能一次读入一个 cirPrompt = 'Input the number of circles:'; cirNum = input(cirPrompt); figure,imshow(img,[]); for i=1:recNum recDefect = drawrectangle(gca,'Color',[0 0 0],'FaceAlpha',1,'linewidth',1); recPos=recDefect.Position; recX=recPos(1);recY=recPos(2);width=recPos(3);height=recPos(4); recGT(recY:recY+height,recX:recX+width)=1;%注意纵横坐标和图像长宽之间的对应关系 end for i=1:cirNum cirDefect=drawcircle(gca,'Color',[0 0 0],'FaceAlpha',1,'linewidth',1); cirX=cirDefect.Center(1);cirY=cirDefect.Center(2);cirR=cirDefect.Radius; [x,y] = meshgrid(1:m,1:n); tmp= (x - cirX).^2 + (y - cirY).^2 <= cirR^2; cirGT=tmp|cirGT; end GT=recGT|cirGT; synDefect(GT==1)=0; %先对缺陷区域添加椒盐噪声 salt_pepper_img=zeros(m,n); salt_pepper_img=imnoise(salt_pepper_img,'salt & pepper',0.05); %加入椒盐躁声 salt_pepper_img(GT~=1)=0; synDefect=synDefect+255*salt_pepper_img;% %对整幅图像添加高斯噪声 SNR=10+(50-10)*rand();%随机生成10-50dB synDefect = awgn(synDefect,SNR); imwrite(uint8(synDefect),strcat(resDir1,D(imgIndex).name)); imwrite(GT,strcat(resDir2,D(imgIndex).name)); close all; end

生成图像示例如下:

如果该代码对您有帮助,欢迎小额赞赏。您的鼓励至关重要!

最后

以上就是感动酸奶最近收集整理的关于Matlab手工拖动鼠标绘制矩形和圆形并产生GroundTruth的全部内容,更多相关Matlab手工拖动鼠标绘制矩形和圆形并产生GroundTruth内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部