How to extract a specific region in matlab ?

2 ビュー (過去 30 日間)
sufian ahmed
sufian ahmed 2017 年 6 月 25 日
コメント済み: Image Analyst 2017 年 6 月 25 日
I know the four coordinates of a trapezium or rectangle.Now i want to only extract this region and other things should be black. How can i do that ?
My test image
I want to do like this image:

回答 (1 件)

Image Analyst
Image Analyst 2017 年 6 月 25 日
Pass the coordinates into poly2mask() and use bsxfun() to mask
% Get the dimensions of the image. numberOfColorChannels should be = 3.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Create mask
mask = poly2mask(x, y, rows, columns);
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
  2 件のコメント
sufian ahmed
sufian ahmed 2017 年 6 月 25 日
why u take numof channels.??. as i saw there is no use here in u r code.. ?
Image Analyst
Image Analyst 2017 年 6 月 25 日
Because if you leave it out,
[rows, columns, numberOfColorChannels] = size(rgbImage);
columns will be the number of columns times the number of color channels. For a full explanation, see http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
Alternatively you could accept the size into a single variable, which will then be a vector but then to get the rows and columns, you will have to add an index 1 or 2 respectively:
% Get the dimensions of the image. numberOfColorChannels should be = 3.
imageSize = size(rgbImage);
% Create mask
mask = poly2mask(x, y, imageSize(1), imageSize (2));
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
I think the way I had it first is easier to understand. You could use ~ instead of numberOfColorChannels if you want:
[rows, columns, ~] = size(rgbImage);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by