MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by yasmine on 22 Oct 2011

i want to resize an RGB image from 512X512 to 256X256 + i want to divide it into 4 equal blocks and take each block and change it from RGB to HSV, all of this using blockproc function or if you can tell me another effective way, here is my code in matlab:

f= imread ('lena_rgb.tiff'); h= rgb2hsv(f); fun = @(block_struct) imresize(block_struct.data,h); I2 = blockproc(f,[2 2],fun); figure(1),imshow(f); figure(2),imshow(I2);

i had all these errors:

??? Function BLOCKPROC encountered an error while evaluating the user supplied function handle, FUN.

The cause of the error was:

Error using ==> imresize>scaleOrSize at 393 Invalid scale or size input argument.

Error in ==> imresize>parsePreMethodArgs at 364 [scale, output_size] = scaleOrSize(next, next_arg);

Error in ==> imresize>parseInputs at 257 [params.A, params.map, params.scale, params.output_size] = ...

Error in ==> imresize at 141 params = parseInputs(varargin{:});

Error in ==> @(block_struct)imresize(block_struct.data,h)

Error in ==> blockprocFunDispatcher at 14 output_block = fun(block_struct);

Error in ==> blockprocInMemory at 71 [ul_output fun_nargout] = blockprocFunDispatcher(fun,block_struct,...

Error in ==> blockproc at 248 result_image = blockprocInMemory(a,block_size,fun,border_size,...

Error in ==> ff4 at 12 I2 = blockproc(f,[2 2],fun);

*No products are associated with this question.*

Answer by Image Analyst on 23 Oct 2011

Accepted answer

Why not just do something simple like

rgbSmall = imresize(rgbBig, 0.5); hsv = rgb2hsv(rgbSmall);

As far as the 4 equal blocks - I have no idea what you're talking about. The whole image gets converted to hsv - every single pixel. So since every single pixel gets converted into hsv, of course each block (quadrant) will also get converted. There is nothing extra that you need to do.

Show 6 older comments

Image Analyst on 29 Oct 2011

Use the code above then do

hsvBlock1 = hsv(1:128, 1:128, :);

hsvBlock2 = hsv(1:128, 129:256, :);

hsvBlock3 = hsv(129:256, 1:128, :);

hsvBlock4 = hsv(129:256, 129:256, :);

yasmine on 29 Oct 2011

this worked well thank you

ok now i want to quantize these HSV values before I build my histogram to reduce memory consumption and processing time. I would like to quantize them to 8(H) x 3(S) x 3(V) bins. Can someone please help me with this? Thanks a lot

Image Analyst on 29 Oct 2011

Find the max value in each array, then divide the pixel value by the max, multiply by the number of levels, and take the ceil. See demo below:

% Generate sample data.

S = magic(6);

% Specify the number of quantization levels.

numberOfLevels = 3;

% Find the max.

maxValue = max(S(:));

% Put all pixels into one of the "numberOfLevels" levels.

for row = 1:size(S, 1)

for col = 1 : size(S, 2)

quantizedValue(row, col) = ceil(numberOfLevels * S(row, col)/maxValue);

end

end

% Display it

quantizedValue

Answer by Walter Roberson on 22 Oct 2011

The second argument to imresize() should be the scale factor that you want to use, such as 0.5

Answer by yasmine on 10 Dec 2011

would you help me to use blockproc() as i tried everything else and didn't work for me

@ Image Analyst: i tried your method but i need the code to be parametric, i don't want to use constant numbers.

Thank you

Answer by Image Analyst on 10 Dec 2011

yasmine: I'm still not sure what you want to do. Sure, I can give you a blockproc demo, if that's what you want. I really don't see why it would be necessary in order to (1) convert your image to hsv, (2) quantize your image into a discrete number of levels, or (3) quantize your histograms into a certain number of bins. I don't even know why you said you want to do those things. You haven't given us the context or "big picture" or what you're trying to do at all, so I can only guess if what you're asking to do is even necessary (my guess is that they're not). Moreover, why pick so few bins: 8(H) x 3(S) x 3(V) bins? I'd make each dimension at least 10 times that big. Why are you higtogramming them anyway? Is it to visualize the histogram so you can pick a threshold? Finally you can replace any constants by variables where you assign the variable to whatever constant you want like instead of hsvBlock1 = hsv(1:128, 1:128, :); you would have col1=1; col2 = 128; row1=1; row2=128; hsvBlock1 = hsv(row1:row2, col1:col2, :); hsvBlock1 = hsv(1:128, 1:128, :);

Show 4 older comments

yasmine on 11 Dec 2011

i used this :

if (sizeOI(1)==256) % 2 by 3

hsvblock1=hsvimage(1:128,1:128,:);

hsvblock2=hsvimage(1:128,129:256,:);

hsvblock3=hsvimage(1:128,257:384,:);

hsvblock4=hsvimage(129:256,1:128,:);

hsvblock5=hsvimage(129:256,129:256,:);

hsvblock6=hsvimage(129:256,257:384,:);

end

if (sizeOI(1)==384) % 3 by 2

hsvblock1=hsvimage(1:128,1:128,:);

hsvblock2=hsvimage(1:128,129:256,:);

hsvblock3=hsvimage(129:256,1:128,:);

hsvblock4=hsvimage(129:256,129:256,:);

hsvblock5=hsvimage(257:384,1:128,:);

hsvblock6=hsvimage(257:384,129:256,:);

end

but for the image 2by3:

it gave me an error:

??? Index exceeds matrix dimensions.

Error in ==> fg at 22

hsvblock3=hsvimage(1:128,257:384,:);

and for block6 also, i can't see an error so what do you think?

Walter Roberson on 11 Dec 2011

Section 3.1 of that paper says,

"3.1 Data set: Wang's [15] dataset comprising of 1000 Corel images with ground truth. The image set comprises 100 images in each of 10 categories. The images are of the size 256 x 384 or 384X256. But the images with 384X256 are resized to 256X384."

Therefore your code only has to deal with images of size 256 x 384, and you should be resizing the 384x256 images to 256 x 384 before entering the part of your code that needs to divide the image up in to blocks.

The question of how to divide up parametrically is thus irrelevant, and a straight blockproc() or mat2cell() can be used.

Answer by Image Analyst on 11 Dec 2011

yasmine: What I was hoping you'd figure out is that you can just do something like

[rows columns numberOfColorBands] = size(hsvimage); halfRows = floor(rows/2); thirdCols = floor(columns/3); block1 = hsvimage(1:halfRows, 1:thirdCols , :); block2 = hsvimage(1:halfRows, thirdCols +1:thirdCols *2, :); block3 = hsvimage(1:halfRows, thirdCols *2+1:end, :); block4 = hsvimage(halfRows+1:end, 1:thirdCols , :); block5 = hsvimage(halfRows+1:end, thirdCols +1:thirdCols *2, :); block6 = hsvimage(halfRows+1:end, thirdCols *2+1:end, :);

Now set up your histogram edges as in the paper and call histc(block1(:), histEdges), or whatever the calling protocol is, for each of the 6 blocks.

yasmine on 15 Dec 2011

i used your code, and now for each block i will do some kind of quantization before calling the histc() function.

I need to make a for loop that repeats the quantization for every block so how to make this for these numeric arrays:

for i=1:6

block(i)(row, col)= "code of quantization"

end

how to make that part "block(i)(row, col)" using matlab ?

Thanks alot

yasmine on 15 Dec 2011

I tried this:

for i=1:6

h = ['Hueblock' int2str(i)];

eval(h)

end

%Quantizing the HUE

for row=1:BRows

for col=1:BCols

if (h(row,col) >= 316 && h(row,col) <= 20)

h(row,col) = 0;

elseif (h(row,col) >= 21 && h(row,col) <= 40)

h(row,col) = 1;

elseif (h(row,col) >= 41 && h(row,col) <= 75)

h(row,col) = 2;

elseif (h(row,col) >= 76 && h(row,col) <= 155)

h(row,col) = 3;

end

but gave me this error:

Attempted to access h(1,10); index out of bounds because size(h)=[1,9].

Error in ==> fg6 at 85

if (h(row,col) >= 316 && h(row,col) <= 20)

## 1 Comment

Direct link to this comment:http://www.mathworks.in/matlabcentral/answers/19053#comment_43329

ok i want to resize the whole RGB image and divide it into 4 blocks using block proc then convert each block to HSV