How to detect smarhtphone's screen in an image
조회 수: 10(최근 30일)
Im trying to write a code which detecting smartphone's screens in an image.
Actually, i would like to get a picture of my desk in which my phone is 2 meters from the camera. The output will be just the phone's screen.
Note that the phone's screen displaying colored image (the next part of my project).
My first idea was to use HOUGH transform, but there are other lines that makes it difficult.
However, I know that the luminance of the screen and the colors are standing out of the other parts in the image.
Example of the input and the desired ouput:
Tarunbir Gambhir 2021년 4월 26일
You can use the regionprops function to get a bounding box for the region of interest in your image. The 'BoundingBox' property of the regionprops function will give you the position and size of the smallest box containing the region.
You can use the following code to get your result.
% loading the image with correct orientation (portrait)
im = rot90(imread('inp.jpeg'),-1);
% binarizing the image (pixel value as 0 or 1)
ib = im2bw(im);
% negating the image so the screen is
% the value 1 and the background is 0
ib = ~ib;
% getting all the possible bounding boxes in the image
% along with their area in pixels
stats = regionprops(ib,'BoundingBox','Area');
% getting indices of the largest 3 white pixel areas
[v,i] = maxk([stats.Area],3);
% displaying their bounding boxes on the image
'EdgeColor','r', 'LineWidth', 3)
% after deciding which bounding box to crop with,
% we can use that to crop the original image.
% In this case we will select the largest area of white pixels,
% which is the phone screen.
imCropped = imcrop(im, stats(i(1)).BoundingBox);