Black shapes against white background

조회 수: 1 (최근 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에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by