如何在 MATLAB 中检测这张图片中某个角度下物体的尺寸? [英] How can I detect the dimensions of an object under an angle in this picture in MATLAB?

查看:32
本文介绍了如何在 MATLAB 中检测这张图片中某个角度下物体的尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这张电池的图片:

我想确定电池的尺寸(以像素为单位).
我的问题是电池旋转了未知角度.
如何检测这个旋转电池的尺寸?

我在考虑这些算法步骤:

  • 首先,我必须将此图像转换为黑白图像(阈值处理).
  • 在那之后,我必须找到一个中心点并在白色像素中绘制一个矩形.
  • 然后,我必须将矩形旋转 360 度并定位矩形的位置(以及尺寸).

我有点经验不足,希望得到有关如何在 Matlab 中实现这些算法阶段的指导.

谢谢

解决方案

将此视为 Matlab 图像处理的初学者教程.阅读所用命令的文档并尝试了解它们在做什么以及为什么.

1.阅读图片

使用 来改善阈值结果.
您可以使用 .它允许您获得各种不错的属性.您将使用它来计算 '图像白色"/电池区域的方向

<代码>>>st = regionprops( bw, '方向' )st =方向:52.8694

如您所见,电池旋转了 52.8 度.
使用 imrotate 来拉直"电池

<代码>>>rbw = imrotate( bw, -st.Orientation );

一旦电池轴对齐,您就可以投影";使用 any<在水平和垂直轴上的白色像素/a>:

<代码>>>pc = any( rbw, 2 );%//将所有行投影到单个列中>>pr = any( rbw, 1 );%//将所有列投影到一行

现在您需要找到投影中设置为 1 的第一个和最后一个像素.使用 find :

<代码>>>fx = find( pr, 1, 'first');%//第一个 x 坐标>>tx = find( pr, 1, 'last');%//最后一个 x 坐标>>fy = find( pc, 1, 'first');%//第一个 y 坐标>>ty = find( pc, 1, 'last');%//最后一个 y 坐标

获得角的 x,y 坐标后,您可以在旋转后的图像上绘制它们:

<代码>>>imshow(rbw,[],'border','tight');>>坚持,稍等;>>plot( [fx tx tx fx fx], [fy fy ty ty fy], ':r', 'LineWidth',3);

产量:

坐标为:

<代码>>>[fx fy tx ty]答案 =406 608 866 733

如您所见,您的电池长 (866-406) 像素,宽 (733-608) 像素.

I have this image of a battery:

I would like to determine the dimensions of the battery (in pixels).
The problem I have is that the battery is rotated by an unknown angle.
How can I detect the dimensions of this rotated battery?

I was thinking of these algorithmic steps:

  • First, I would have to convert this image to a black and white image (thresholding).
  • After that, I would have to find a center point and draw a rectangle in the white pixels.
  • Then, I have to turn the rectangle 360 degrees and locate the position of the rectangle (and so the dimensions).

I am somewhat inexperienced, and I would appreciate some guidance as to how to implement these algorithmic stages in Matlab.

Thanks

解决方案

Consider this as a beginner's tutorial to Matlab image processing. Read the documentation of the commands used and try and understand what they are doing and why.

1. Read the image

Use imread to read the image into a 3D matrix. For convenience, we convert it to double in the range [0..1] using im2double:

>> img = im2double( imread( 'path/to/battety.jpg' ) );

You can check out the size of your img using size command:

>> size( img )
ans =
    1024         768           3

You can see from the result that your image has 1024 rows, 768 columns and 3 channels (Red, Green and Blue).

2. Convert to black and white (a.k.a thresholding)

As you can see the battery is significantly brighter than the background and is colorless. We can select pixels that have large gap between the brightest channel value to darkest channel values as "battery" pixels:

>> bw = (max(img,[],3)-min(img,[],3)) > 0.2;

See max and min for more details.
There are other methods to threshold an image, see graythresh for more details.

Using imshow we can see what we got:

>> imshow(bw,[],'border','tight');

Normally one uses morphological operations to improve thresholding results.
You can use imclose:

>> bw = imclose( bw, ones(25) );

Resulting with:

3. find angle of rotation

A very useful command for processing and working with bw images is regionprops. It allows you to get all sorts of nice properties. You are going to use it to compute the 'Orientation' of the "white"/battery region of your image

>> st = regionprops( bw, 'Orientation' )
st = 
Orientation: 52.8694

As you can see the battery is rotated by 52.8 degrees.
Using imrotate to "straighten" the battery

>> rbw = imrotate( bw, -st.Orientation );

Once the battery is axis-aligned, you can "project" the white pixels onto the horizontal and vertical axes using any:

>> pc = any( rbw, 2 ); %// project all rows into a single column 
>> pr = any( rbw, 1 ); %// project all columns into a single row

Now you need to find the first and last pixels set to 1 in the projections. Use find for that:

>> fx = find( pr, 1, 'first');  %// first x coordinate
>> tx = find( pr, 1, 'last');   %// last x coordinat
>> fy = find( pc, 1, 'first');  %// first y coordinate
>> ty = find( pc, 1, 'last');   %// last y coordinate  

Once you have x,y coordinates of corners, you can plot them on the rotated image:

>> imshow(rbw,[],'border','tight');
>> hold on; 
>> plot( [fx tx tx fx fx], [fy fy ty ty fy], ':r', 'LineWidth',3);

Yields:

And the coordinates are:

>> [fx fy tx ty]
ans =
406   608   866   733

As you can see your battery is (866-406) pixels long and (733-608) pixels wide.

这篇关于如何在 MATLAB 中检测这张图片中某个角度下物体的尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆