select particular 8 *8 blocks then change the pixel values of this blocks to 0

I divided an image to 8*8 blocks, then I want to select particular blocks and change the pixel values of this blocks to 0, the index of blocks to select are for example v=[1,23,25,87,96].
Can anyone help me please

Answers (1)

Assuming you have numbered the blocks row-wise.
You can simply crop any indexed block likw this:
Im is the main image
Block_Index = 23;
Dividing_Factor = size(Im,2)/8;
R1 = 1 + 8*(floor(Block_Index-1./Dividing_Factor));
R2 = R1 +7;
C1 = 1 + 8*(mod(Block_Index-1,Dividing_Factor));
C2 = C1 +7;
Im(R1:R2,C1:C2) = 0; % Or any value you want to give
I hope this is the solution you were finding.

7 Comments

Thank you for your answer.
The size of my original image is [512 512], I used your code in loop for because i have many blocks to change but the size of the image has been changed to [520 512]
for i=1:length(v_index)
Block_Index= v_index(i);
Dividing_Factor = size(Im,2)/8;
R1 = 1 + 8*(floor(Block_Index./Dividing_Factor));
R2 = R1 +7;
C1 = 1 + 8*(mod(Block_Index,Dividing_Factor));
C2 = C1 +7;
Im(R1:R2,C1:C2) = 0; % Or any value you want to give
end
Please give complete code using either your image or a demo image, such as moon.tif or cameraman.tif. And specify if your block numbering goes top-to-bottom then left-to-right (column major order), OR left-to-right, then down top-to-bottom (row major order).
Hey I just missed a (-1) there, Check this Code now. It will work.
for i=1:length(v_index)
Block_Index= v_index(i);
Dividing_Factor = size(Im,2)/8;
R1 = 1 + 8*(floor((Block_Index-1)./Dividing_Factor));
R2 = R1 +7;
C1 = 1 + 8*(mod(Block_Index-1,Dividing_Factor));
C2 = C1 +7;
Im(R1:R2,C1:C2) = 0; % Or any value you want to give
end
And yes this code assumes that your numbering goes first left-to-right then down top-to-bottom - Row major order
I tested the code on matrix 16*16
newBloc= repelem(5,64);
newBloc=reshape(newBloc,[8 8]);
newBloc2= repelem(225,64);
newBloc2=reshape(newBloc2,[8 8]);
newBloc3= repelem(100,64);
newBloc3=reshape(newBloc3,[8 8]);
newBloc4= repelem(36,64);
newBloc4=reshape(newBloc4,[8 8]);
matrix=[newBloc newBloc2;newBloc3 newBloc4]
Block_Index = 1;
Dividing_Factor = 16/8;
R1 = 1 + 8*(floor(Block_Index./Dividing_Factor));
R2 = R1 +7;
C1 = 1 + 8*(mod(Block_Index,Dividing_Factor));
C2 = C1 +7;
matrix(R1:R2,C1:C2) =0 %
matrix(R1:R2,C1:C2) returns the second bloc, not the first one.
How I can fix it.
@Image Analyst
I used lena image (512*512), please find attached the code to divide the image into 8*8 blocks. what i need is to select particular blocks and change them to 0. The index of blocks to select are for example
v=[1,23,25,87,96].
Thanks in advance for your answer.
Initially you were divind in 8*8 blocks so the code was written accordingly, If you need it for 16*16 blocks then definately you will have to make some changes, same code won't work.
Changes for dividing te image in NxN blocks:
Dividing_Factor = size(Im,2)/N;
R1 = 1 + (N-1)*(floor((Block_Index-1)./Dividing_Factor));
R2 = R1 +(N-1);
C1 = 1 + N*(mod(Block_Index-1,Dividing_Factor));
C2 = C1 +(N-1);

Sign in to comment.

Categories

Asked:

on 21 Dec 2020

Commented:

on 11 Jan 2021

Community Treasure Hunt

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

Start Hunting!