I just thought it was fun to write such a program.
The principle is to randomly generate two points, draw a straight line based on the two points, and retain the pixels in the straight line. The pixels in the straight line will not be discarded.
Finally, an image containing many holes is generated.
Of course, the repair algorithm can be used to fix images containing holes.
I also tried to fix it. I used to write the algorithm. You can check it here.
This time, the repair code will not be pasted. The input image IMG, mask and the output image IMG and mask in that program are exactly the same.
Source image:
After sampling:
After repair:
The Matlab code is as follows:
Main. M:
clear all; close all;clc;img=double(imread(‘lena.jpg‘));imshow(img,[]);[h,w]=size(img);n=200;pix=rand(n,4);pix(:,1:2)=round(pix(:,1:2)*(w-1))+1;pix(:,3:4)=round(pix(:,3:4)*(h-1))+1;mask=zeros(h,w);for i=1:n x1=pix(i,1);x2=pix(i,2); y1=pix(i,3);y2=pix(i,4); mask=drawline(mask,x1,y1,x2,y2); endind=find(mask~=1);img(ind)=0;figure;imshow(img,[])
Drawline. M:
Function IMG = drawline (IMG, X1, Y1, X2, Y2) % because the Y axis of the image coordinate is opposite to that of the mathematical function coordinate, so here all the Y variables take the opposite number [H w] = size (IMG); Y1 =-Y1; y2 =-Y2; If X1 ~ = X2 k = (y2-y1)/(x2-x1); B = y1-k * x1; for I = 1: w yy =-round (I * k + B); xx = I; if YY> = 1 & YY <= H & XX> = 1 & XX <= w img (YY, XX) = 1; end if Y1 ~ = Y2 k = (x2-x1)/(y2-y1); B = x1-k * Y1; for I =-H: 1 YY =-I; xx = round (I * k + B); If YY> = 1 & YY <= H & XX> = 1 & XX <= W IMG (YY, XX) = 1; end
MATLAB exercise program (Random Linear sampling)