Black shapes against white background
1 次查看(过去 30 天)
显示 更早的评论
I am trying to process images of 3d blocks so that the side facing the camera becomes black against a white background, just like below
But when I run my code I get the result below
Does anybody know what I should do? Here's the main loop of my code:
for i = 1:numel(imageFiles)
imagePath = fullfile(imageFolderPath, imageFiles(i).name);
originalImage = imread(imagePath);
grayImage = rgb2gray(originalImage);
darkThreshold = 78;
redMask = originalImage(:,:,1) < darkThreshold;
greenMask = originalImage(:,:,2) < darkThreshold;
blueMask = originalImage(:,:,3) < darkThreshold;
binaryMask = ~(redMask | greenMask | blueMask);
binaryMask = imopen(binaryMask, strel('disk', 20));
binaryMask = imclose(binaryMask, strel('disk', 15));
outputImage = uint8(cat(3, 255 * binaryMask, 255 * binaryMask, 255 * binaryMask));
figure;
subplot(1, 2, 1), imshow(originalImage), title('Original Image');
subplot(1, 2, 2), imshow(outputImage), title('Processed Image');
end
1 个评论
DGM
2024-3-18
编辑:DGM
2024-3-18
This is where having a telecentric lens would be helpful, though it's hard to say that it's warranted without knowing more about the actual task.
Otherwise, attach examples of the various images from the set, as any applied code would have to accomodate the variation within the set. A single screenshot isn't really sufficient.
回答(1 个)
Aishwarya
2024-3-18
Based on the information provided, I understand that you want to process the image in such a way that the 3D blocks are represented as 2D black shapes on a white background.
After reviewing the code, below are a few suggestions that could help in getting the desired result:
- Adjust Structuring Element Size: As the image shows the edges are overly smoothed and close objects are merged, this could be because the size of the structuring element might be large for your images. Consider reducing the size of the structuring element used in the “imopen” and “imclose” functions as shown in the code snippet below.
binaryMask = imopen(binaryMask, strel('disk', 5)); % Adjust the structuring element size
binaryMask = imclose(binaryMask, strel('disk', 5)); % Adjust the structuring element size
- Review the Morphological Operations: Experiment with other morphological operations like “imerode” and “imdilate” with different structuring elements. Using “imerode” and “imdilate” functions separately allows for more customized morphological adjustments.
- Consider Adaptive Thresholding: Try using adaptive thresholding techniques (like “addatthresh” and “imbinarize”), which might better accommodate varying lighting conditions across different parts of the image. An example implementation of adaptive thresholding is shown below, adjust the parameters according to your images and desired result.
% Adaptive Thresholding
T = adaptthresh(adjustedImage, 0.4, 'ForegroundPolarity','dark');
binaryMask = imbinarize(adjustedImage, T);
You can refer to the below documentation for more information about the functions used:
- "imerode" function: https://www.mathworks.com/help/images/ref/imerode.html
- "imdilate" function: https://www.mathworks.com/help/images/ref/imdilate.html
- "adaptthresh" function: https://www.mathworks.com/help/images/ref/adaptthresh.html
- "imbinarize" function: https://www.mathworks.com/help/images/ref/imbinarize.html
I hope this information is helpful!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!