How to define Block Size Based on Input Image Dimensions in BLOCKPROC with SEMANTICSEG()

3 views (last 30 days)
Dear all,
I am using the BLOCKPROC command to segment an input image (21828x54644x3) by passing blocks to the SEGMANTICSEG() function. In this case, I found that the smallest block size that executes without error is [2048,2048]. However, when using an even larger input image (22609x24064x3), the command does not execute if I keep the same block size of [2048,2048]. I need to increase the block size slightly, and it worked with a block size of [2100,2100]. I would like to understand the reason for this and how I can determine the smallest block size based on the image size. Could someone help me?
Thank you.
IMG = imread('image.tif');
BlockSize = [2048 2048];
fun_proc_semanticseg = @(bloco)semanticseg(bloco.data, net, ExecutionEnvironment="gpu",
MiniBatchSize=16,OutputType="uint8");
C = blockproc(IMG, BlockSize, fun_proc_semanticseg);

Answers (2)

Umar
Umar on 6 Jul 2024
Hi Airton,
Based on your description, it seems that you are encountering an issue with the block size parameter when using the BLOCKPROC command to segment images with the SEMANTICSEG() function. The error you faced with a larger image size (22609x24064x3) while using a block size of [2048,2048] suggests that the block size may need adjustment based on the image dimensions to avoid errors.The reason behind this discrepancy lies in the relationship between the image size, block size, and how they interact during processing. When dividing a larger image into blocks for segmentation, the block size needs to be chosen carefully to ensure that each block fits within the image dimensions without causing issues such as out-of-bound errors or incomplete processing.
To determine the optimal block size based on the image dimensions, you can calculate it by considering factors such as the total number of pixels in the image and how many blocks can be evenly divided without exceeding the image boundaries. In your case, increasing the block size slightly to [2100,2100] for the larger image resolved the issue because it better aligned with the image dimensions and prevented processing errors.
To address this problem in your code and optimize the block size selection process, you can implement a dynamic calculation of the block size based on the image dimensions. This can be achieved by determining the maximum block size that fits within the image dimensions while ensuring complete coverage without overlapping or exceeding boundaries.Here is an example of how you can dynamically adjust the block size based on the image dimensions in MATLAB:
IMG = imread('image.tif');
imageSize = size(IMG);
% Choose the smaller dimension as the maximum block size
maxBlockSize = min(imageSize(1:2));
for blockSize = maxBlockSize:-1:1 if mod(imageSize(1), blockSize) == 0 && mod(imageSize(2), blockSize) == 0 BlockSize = [blockSize blockSize]; break; end end
fun_proc_semanticseg = @(bloco)semanticseg(bloco.data, net, ExecutionEnvironment="gpu", MiniBatchSize=16,OutputType="uint8");
C = blockproc(IMG, BlockSize, fun_proc_semanticseg);
By dynamically calculating the optimal block size based on the image dimensions, you can ensure efficient and error-free segmentation of images using BLOCKPROC and SEMANTICSEG functions. This approach allows for adaptive adjustment of the block size to accommodate different image sizes while maintaining processing integrity.
For more information on these functions, please refer to
https://www.mathworks.com/help/images/ref/blockproc.html
https://www.mathworks.com/help/vision/ref/semanticseg.html
Hope this will help resolve your problem.
  1 Comment
Airton Gaio Junior
Airton Gaio Junior on 6 Jul 2024
Dear Umar,
Thank you very much for the quick response!
The code you provided indeed finds the smallest block size. I ran it for an input image (21828x54644) and it found a block size of 4. However, the block size I'm looking for should be as close as possible to 512 or greater because my CNN network was trained with a block size of 512x512. So what I really need is a suitable block size for the image that is as close as possible to 512. I attempted to stop the loop at 512, but it didn't work:
Is there anything else you would like to adjust or add?
for blockSize = maxBlockSize:-1:512
if mod(imageSize(1), blockSize) == 0 && mod(imageSize(2), blockSize) == 0
BlockSize = [blockSize blockSize];
break;
end
end

Sign in to comment.


Umar
Umar on 6 Jul 2024

Hi Airton,

To address your specific requirement, you can modify the code snippet provided by adjusting the loop condition to ensure that the block size found is as close to 512 as possible. One way to achieve this is by calculating the absolute difference between the current block size and 512, aiming to minimize this difference. Here's an example of how you can adjust the code:

% Define the image size imageSize = [21828,54644]; % Example dimensions

% Define the target block size targetSize = 512;

% Define the initial maximum block size maxBlockSize = 512; % Assuming an initial value

% Initialize with the maximum possible block size bestBlockSize = maxBlockSize;

for blockSize = maxBlockSize:-1:512 if mod(imageSize(1), blockSize) == 0 && mod(imageSize(2), blockSize) == 0 if abs(blockSize - targetSize) < abs(bestBlockSize - targetSize) bestBlockSize = blockSize; end end end

% Final block size closest to 512 BlockSize = [bestBlockSize bestBlockSize];

% Display the closest block size found disp(BlockSize);

Please see attached results.

By incorporating this adjustment, the code will now iteratively search for a block size that is as close as possible to 512 while ensuring it fits the image dimensions correctly. This approach should help you find a suitable block size for your image based on your specified requirements.

If you encounter any further challenges or require additional customization, feel free to provide more details so we can tailor the solution accordingly. Let me know if there are any other aspects you would like to adjust or add to refine the process further.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!