trying to make a donut mask to span over images, suggestions?

12 visualizzazioni (ultimi 30 giorni)
[x,y] = meshgrid(1:width, 1:height);
centerX = (width + 1) / 2;
centerY = (height + 1) / 2;
dist = sqrt((x - centerX).^2 + (y - centerY).^2);
innerRadius = 50;
outerRadius = 100;
masksign = false(height, width);
masksign(dist >= innerRadius & dist <= outerRadius) = true;
masksign = grayImage >= lowThreshold & grayImage <= highThreshold;
imshow(masksign);

Risposta accettata

Kevin Holly
Kevin Holly il 23 Feb 2023
RGBImage = imread("peppers.png");
imshow(RGBImage)
size(RGBImage)
ans = 1×3
384 512 3
width = 300;
height = 300;
[x,y] = meshgrid(1:width, 1:height);
centerX = (width + 1) / 2;
centerY = (height + 1) / 2;
dist = sqrt((x - centerX).^2 + (y - centerY).^2);
innerRadius = 50;
outerRadius = 100;
masksign = false(height, width);
masksign(dist >= innerRadius & dist <= outerRadius) = true;
% masksign = grayImage >= lowThreshold & grayImage <= highThreshold;
figure
imshow(masksign);
size(masksign)
ans = 1×2
300 300
figure
imshow(uint8(masksign).*RGBImage(1:300,1:300,:))
  3 Commenti
Kevin Holly
Kevin Holly il 24 Feb 2023
I am not sure what exactly you are trying to accomplish. Below, I rotated the images you create to show a half donut halved vertically.
width = 300;
height = 300;
% Create a meshgrid
[x,y] = meshgrid(1:width, 1:height);
% Set the center coordinates of the donut
centerX = (width + 1) / 2;
centerY = (height + 1) / 2;
% Calculate the distance of each point from the center
dist = sqrt((x - centerX).^2 + (y - centerY).^2);
% Set the radii of the inner and outer circles
innerRadius = 50;
outerRadius = 100;
% Create a logical array for the mask
masksign = false(height, width);
% Set the region between the radii to true
masksign(dist >= innerRadius & dist <= outerRadius) = true;
% Set the region above the center to false
masksign(1:round(centerY),:) = false;
% Display the mask
figure
imshow(masksign);
stacked_vertically = [];
% Define the number of half donut masks to create
numMasks = 9;
% Define the size of the image
width = 300;
height = 300;
% Calculate the width of each mask
maskWidth = floor(width / numMasks);
% Create a meshgrid
[x,y] = meshgrid(1:width, 1:height);
% Loop through each mask and create a half donut mask
for i = 1:numMasks
% Calculate the center coordinates of the mask
centerX = (maskWidth * (i - 0.5));
centerY = (height + 1) / 2;
% Calculate the distance of each point from the center
dist = sqrt((x - centerX).^2 + (y - centerY).^2);
% Set the radii of the inner and outer circles
innerRadius = 50;
outerRadius = 100;
% Create a logical array for the mask
% masksign = false(height, maskWidth);
% Set the region between the radii to true
masksign = dist >= innerRadius & dist <= outerRadius;
% Set the region above the center to false
masksign(1:round(centerY),:) = false;
% Display the mask
subplot(2,numMasks,i);
imshow(masksign);
% Display the mask
subplot(2,numMasks,i+numMasks);
imshow(imrotate(masksign,90));
stacked_vertically = [stacked_vertically ; imrotate(masksign,90)];
end
figure
imshow(stacked_vertically)
Neo
Neo il 24 Feb 2023
Hi Dr. @Kevin Holly Thank you for this it really helped.
I am trying to get my attached script to work.
My code is attempting to count the number of dots (using a left half donut shaped mask) over 12 (or 9 the number is arbitrary) consecutive locations evenly distributed across the image and plot the count of the content as a function of the location.
My issues are the integer values are either not logical, or not positive and my code doesn't like that. Everytime I think I fix it, some other related error pops up. I thought the issue was the mask, then the maxDistance but now i am unsure. What would you suggest? Thank you.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by