How to detect smarhtphone's screen in an image
2 views (last 30 days)
Show older comments
Itzhak Mamistvalov
on 23 Apr 2021
Commented: Itzhak Mamistvalov
on 27 Apr 2021
Hey everyone.
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.
Thanks.
Example of the input and the desired ouput:
0 Comments
Accepted Answer
Tarunbir Gambhir
on 26 Apr 2021
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);
imshow(im);
% 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;
imshow(ib)
hold on
% 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
for ind=1:length(i)
rectangle('Position', stats(i(ind)).BoundingBox,...
'EdgeColor','r', 'LineWidth', 3)
end
% 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);
figure;
imshow(imCropped);
More Answers (0)
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!