Before lens detection, we extract a certain number of image sequences from a video clip for convenience.
% Extract Image Sequence % Video = mmreader ('test. avi', 'tag', 'reader'); nof = video. numberofframes; img_diff = zeros (NOF-1, 1); mkdir ([cd, '/images']); directory = [cd, '/images/']; for I = 1: NOF img_ I = read (video, I); imwrite (img_ I, [directory, [num2str (I) '.jpg '];]); end;
The lens edge detection algorithm is used to detect image frames with a lens switching in a video clip. Generally, adjacent image frames are similar in the same lens, so we can detect sudden changes in the features of adjacent image frames. The following are typical algorithms. These algorithms are inseparable from two important points,How to define image features,How to determine Similarity.
Absolute Frame Difference Method:Compare the brightness and difference of adjacent image frames. When the brightness is greater than a threshold value, it is determined that a mutation occurs. The selection of this algorithm threshold will affect the accuracy. There should be room for improvement in determining the threshold. Here, we simply take the global average of 1.1 times as the threshold.
for i=1:NOF-1 img_i=imread(strcat('images\',imglist(i).name)); img_i_plus=imread(strcat('images\',imglist(i+1).name)); img_diff(i)=norm(double(img_i(:,:,1)-img_i_plus(:,:,1)))+norm(double(img_i(:,:,2)-img_i_plus(:,:,2)))+norm(double(img_i(:,:,3)-img_i_plus(:,:,3)));end;Threshold=mean(img_diff)*1.1;for i=2:NOF-2 if(img_diff(i)>img_diff(i-1)&&img_diff(i)>img_diff(i+1)&&img_diff(i)>Threshold) fprintf('%d\n',i); end;end;
Color histogram method:This algorithm uses the color histogram of the image as the image feature and uses the intersection of histograms to measure the similarity between images. When the similarity is lower than a threshold, it is determined to be a mutation. The following is the definition of histogram intersection. In fact, the definition of similarity is flexible. Here, we only provide an idea.
clc;clear;clf;d=dir('images');NOF=max(size(d)-2);img_diff=zeros(NOF-1,1);imglist=d(3:NOF+2);Threshold=0.45;for i=1:NOF-1 img_i=imread(strcat('images\',imglist(i).name)); img_i_plus=imread(strcat('images\',imglist(i+1).name)); Hist1=imhist(rgb2gray(img_i)); Hist2=imhist(rgb2gray(img_i_plus)); S=min(Hist1(1),Hist2(1)); for j=2:length(Hist1) S=S+min(Hist1(j),Hist2(j)); end; H=sum(Hist1); img_diff(i)=S/H;end;for i=1:length(img_diff) if(img_diff(i)<Threshold) fprintf('%d\n',i); end;end;
Perception hashing:Perceptual hashing is a fast algorithm used for searching similar images. As the camera detection algorithm targets thousands or even hundreds of frames, I try to use this algorithm for lens detection.
clc;clear;clf;d=dir('images');NOF=max(size(d)-2);;imglist=d(3:NOF+2);Threshold=6;count=zeros(NOF-1,1);for i=1:NOF-1 img_i=imread(strcat('images\',imglist(i).name)); img_i_plus=imread(strcat('images\',imglist(i+1).name)); imbw_i=im2bw(rgb2gray(imresize(img_i,[8,8]))); imbw_i_plus=im2bw(rgb2gray(imresize(img_i_plus,[8,8]))); for j=1:8 for k=1:8 if(imbw_i(j,k)~=imbw_i_plus(j,k)) count(i)=count(i)+1; end; end; end; end;for i=1:NOF-2 if(count(i)>Threshold) fprintf('%d\n',i); end;end;
Image Correlation coefficient method:We know that the correlation coefficient is used to represent the correlation between two random variables in mathematics. I tried to define the correlation coefficient of two images to measure the similarity between adjacent image frames.
clc;clear;clf;d=dir('images');NOF=max(size(d)-2);img_sim=zeros(NOF-1,1);imglist=d(3:NOF+2); for i=1:NOF-1 img_i=imread(strcat('images\',imglist(i).name)); img_i_plus=imread(strcat('images\',imglist(i+1).name)); img_sim(i)=corr2(img_i(:,:,1),img_i_plus(:,:,1))+corr2(img_i(:,:,2),img_i_plus(:,:,2))+corr2(img_i(:,:,3),img_i_plus(:,:,3)); img_sim(i)=img_sim(i)/3;end;Threshold=0.05;for i=1:length(img_sim) if(img_sim(i)<Threshold) fprintf('%d\n',i); end;end;
After testing, it is found that although the above algorithms can roughly detect some lenses, the accuracy is not very satisfactory, especially for gradient lenses. In general, these algorithms are too rough for the reason. One is that the feature definition is too simple to fully reflect the image features. In addition, the definition or threshold of the similarity function (adaptive threshold can be considered) needs to be improved.