現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi,
I want to draw grid on an image and I want to count the number of grid boxes which cover an object in that image. Some regions of that object intersect the grid. How can I count those intersect grids as well as inside of that object.?
Experts kindly get me the solution.
Regards Dams
採用された回答
Image Analyst
2013 年 5 月 11 日
Damodara:
If you draw C lines along the columns (vertically), this will split the image into (C+1) vertical strips. If you draw R lines along the rows (horizontally), this will split the image into (R+1) horizontal strips. You will then have (C+1)*(R+1) boxes in the grid. You will have C*R intersections, where the vertical and horizontal lines intersect.
If you have a binary image then you need to make a mask of the lines and multiply it or assign the binary image to be false there. Then call bwlabel to get the number of parts you chopped your object into.
[labeledImage, numberOfPieces] = bwlabel(binaryImage);
Of course that depends on where you drew your lines and will have a problem if any of the lines overlap a 1 pixel wide part. Please run this demo I made specially for you:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Synthesize an image.
grayImage = peaks(400);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage)
% Display the original gray scale image.
fh = figure;
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Binarize the image by thresholding it.
binaryImage = grayImage > 2.1;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Make mask
columnSplitters = 16:32:columns;
rowSplitters = 16:32:rows;
splitBinaryImage = binaryImage; % Initialize.
splitBinaryImage(:, columnSplitters) = false;
splitBinaryImage(rowSplitters, :) = false;
% Display the image.
subplot(2, 2, 3);
imshow(splitBinaryImage, []);
title('Split Binary Image', 'FontSize', fontSize);
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(splitBinaryImage, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 2, 4);
imshow(coloredLabelsImage);
caption = sprintf('The number of pieces = %d', numberOfBlobs)
title(caption, 'FontSize', fontSize);
message = sprintf('Done with Image Analyst demo.\nThe number of pieces = %d', numberOfBlobs);
uiwait(helpdlg(message));
close(fh); % Close down the figure

12 件のコメント
Image Analyst
2013 年 5 月 11 日
P.S. Note that all the slicing lines are not shown in the lower left image due to subsampling (shrinking) of the image.
Damodara
2013 年 5 月 12 日
編集済み: Image Analyst
2013 年 5 月 12 日
Thanks but here if you reduce row and column splitters to one it will not give any count. And even mask lines take some pixels so count will be wrong if you consider more lines. My code as here. Kindly check this and how can I count these pieces.? Please help me.
myFolder =uigetdir;
%....GET INPUT DIRCTORY......
filePattern = fullfile(myFolder, '*.png');
pngFiles = dir(filePattern);
for i = 1:length(pngFiles)
baseFileName = pngFiles(i).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
binaryImage(:,:,i)=imread(fullFileName);
imagesc(binaryImage);
colormap(gray)
[nX,nY,nZ]=size(binaryImage);
nSeg=max(nX,nY);
set(gca,'xtick',linspace(0,nY,nSeg+1),'xticklabel',[],...
'xgrid','on','xcolor','K',...
'ytick',linspace(0,nX,nSeg+1),'ytickLabel',[],...
'ygrid','on','ycolor','K',...
'gridLineStyle','-','linewidth',1)
end
Regards Dams
Image Analyst
2013 年 5 月 12 日
But, unlike my code, your code doesn't do anything. It just reads in images, and even then it requires that they all be the same size. Did you forget to adapt my code and paste it in? And I warned you about the line taking up a pixel and thus would need to be adapted some if you expect to have one pixel wide regions. And of course, you need to specify exactly what rows and columns you want to split the image on - recall I said it depends on where you draw your lines. I assume you know that, and know that I just made some arbitrary decisions on that, but it looks like I better make that crystal clear.
Damodara
2013 年 5 月 12 日
Yes I have tried your code. Its good what I expected. But again I am facing bit problem how can I get one pixel regions from your code.?Suppose an object has 756 pixels in an image. If I split that object to one pixel regions then it has to give 756. Is there any way to split without considering mask lines because it takes a pixel to draw a line as like mentioned in above code just draw grid and how many regions are there .?
Regards Dams
Image Analyst
2013 年 5 月 12 日
I thought you wanted grid boxes over the object - that's what you said. If you want individual pixels, then the image is already in that form. Each element is a single pixel.
If you have an object, you can get the individual pixel coordinates using the PixelList measurement returned by regionprops(). You can get the pixel values using the PixelValues measurement returned by regionprops().
Yes, I wanted grid boxes over the object and count number of grid boxes which cover only that object. Even I wanted to increase grid size from one pixel to size of that image that is where I stuck up. I can increase grid size in loop but how to count which covers only that object. ?
Image Analyst
2013 年 5 月 12 日
I don't understand what you are asking. Time for you to upload a screenshot.
Damodara
2013 年 5 月 13 日
Hi, How can I upload my screenshot here. ?
Regards Dams
Image Analyst
2013 年 5 月 13 日
Activate your window, type alt-printscreen, go to http://snag.gy and type control-v. Then tell me the URL that you see.
Damodara
2013 年 5 月 14 日
Hi,
How can count these grid boxes which cover that object. I wanted to count for different grid size. http://snag.gy/VOThM.jpg
Regards Dams
Image Analyst
2013 年 5 月 14 日
How is that different than what I did? And your boxes are not one pixel wide. The grid lines are, but the boxes are not -they're wider than that, just like I had. What's the use of chopping your object up like this anyway?
Damodara
2013 年 5 月 14 日
Hi,
Here grid size is not one pixel. Have tried my code which i posted.?I have given an example. I want to use it for fractal dimension. Regards Dams
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Convert Image Type についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
