# how to divide an RGB image using blockproc function

4 views (last 30 days)

Show older comments

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);

### Accepted Answer

Image Analyst
on 23 Oct 2011

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.

##### 9 Comments

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

### More Answers (4)

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

##### 0 Comments

Image Analyst
on 10 Dec 2011

##### 7 Comments

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.

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.

##### 3 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!