Black shapes against white background

2 次查看(过去 30 天)
Kacper
Kacper 2024-3-8
编辑: DGM 2024-3-18
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
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
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:
I hope this information is helpful!

类别

Help CenterFile Exchange 中查找有关 3-D Volumetric Image Processing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by