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

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

New to MATLAB?

color image into blocks

Asked by Elysi Cochin

Elysi Cochin (view profile)

on 7 Jan 2013
Accepted Answer by Image Analyst

Image Analyst (view profile)

i wrote a code to convert color image into blocks... the output looks like the input image in grayscale with gridlines over it... can i get the output with gridlines over the color input image i give.... please do reply

```    rgbimage = imread('peppers.png');
patchsize = 10;
imsize = 10*25;
rgbimage = imresize(rgbimage ,[imsize imsize]);
image_out = [];
for i = 1:patchsize:imsize
image_out_tmp = [];
for j = 1:patchsize:imsize
tmp_img = rgbimage ((i+1-1):i+patchsize-1,(j+1-1):j+patchsize-1);
tmp_img = [ones(patchsize,1) tmp_img ones(patchsize,1)];
tmp_img = [ones(1,patchsize+2);tmp_img;ones(1,patchsize+2)];
image_out_tmp = [image_out_tmp tmp_img];
end
image_out = [image_out;image_out_tmp];
end
figure;imshow(image_out);```

1 Comment

Walter Roberson

Walter Roberson (view profile)

on 7 Jan 2013

Why are you naming a variable "input"? I know we have advised you in the past to not name variables the same thing as key MATLAB functions.

Products

No products are associated with this question.

Answer by Image Analyst

Image Analyst (view profile)

on 7 Jan 2013
```% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
```
```% Now draw your lines in the channels for every row and column that you want.
redChannel(row, :) = 0;
greenChannel(row, :) = 0;
blueChannel(row, :) = 0;
redChannel(:, column) = 0;
greenChannel(:, column) = 0;
blueChannel(:, column) = 0;
```
```% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
```

Elysi Cochin

Elysi Cochin (view profile)

on 7 Jan 2013

sir i'm getting just the input image as output... and the remaining part of the figure towards the right is black

Image Analyst

Image Analyst (view profile)

on 7 Jan 2013

How can that possibly be? Did you give an array or a number for row and column? What rows and columns do you want black lines drawn in? In the code below, I did just what I said and it works fine. What did you do differently? (Note: this will overwrite some of the rows and columns of the original image with black pixels. Hopefully that will be no big concern.)

```clc;	% Clear command window.
clear;	% Delete all variables.
close all;	% Close all figure windows except those created by imtool.
workspace;	% Make sure the workspace panel is showing.
fontSize = 15;
```
```% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there.  Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it.  Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Get the dimensions of the image.  numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
```
```% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
```
```% Specify grid lines:
row = 1:32:rows;
column = 1: 32 : columns;
```
```% Now draw black lines in the channels for every row and column that you want.
redChannel(row, :) = 0;
greenChannel(row, :) = 0;
blueChannel(row, :) = 0;
redChannel(:, column) = 0;
greenChannel(:, column) = 0;
blueChannel(:, column) = 0;% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 2, 2);
imshow(rgbImage, []);
title('Color Image with Black Grid Lines', 'FontSize', fontSize);
```
```% Now draw white lines in the channels for every row and column that you want.
redChannel(row, :) = 255;
greenChannel(row, :) = 255;
blueChannel(row, :) = 255;
redChannel(:, column) = 255;
greenChannel(:, column) = 255;
blueChannel(:, column) = 255;% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 2, 3);
imshow(rgbImage, []);
title('Color Image with White Grid Lines', 'FontSize', fontSize);
```
Elysi Cochin

on 8 Jan 2013

thank u sir...

Image Analyst (view profile)

Answer by Gamil Qaid

Gamil Qaid (view profile)

on 23 Apr 2013

clear all; A = imread('lenaT.jpg');% size of image 512 x 512 [r c]=size(A); bs= 256; % Block Size (256x256)

nob=(r/bs)*(c/bs); % Total number of 256x256 Blocks

% Dividing the image into 4x4 Blocks kk=0; for i=1:(r/bs) for j=1:(c/bs) Block(:,:,kk+j)=A((bs*(i-1)+1:bs*(i-1)+bs),(bs*(j-1)+1:bs*(j-1)+bs)); end kk=kk+(r/bs);

end

```    A1 = (Block(:,:,1));
A2 = (Block(:,:,2));
A3 = (Block(:,:,3));
A4 = (Block(:,:,4));```

figure(1) subplot(2,2,1);imshow(A);title('Original Image'); subplot(2,2,2);imhist(A);title('histogram of the Orignal Image'); figure(2) subplot(2,2,1);imshow(A1);title('First Block'); subplot(2,2,2);imshow(A2);title('Secound Block'); subplot(2,2,3);imshow(A3);title('Third Block'); subplot(2,2,4);imshow(A4);title('Fourth Block');

Gamil Qaid (view profile)

Answer by Walter Roberson

Walter Roberson (view profile)

on 7 Jan 2013

Why are you only accessing two dimensions of the three-dimensional array you named "image" ?

People would not usually refer to what you are doing as converting the image into "patches": they would talk about converting it into "blocks". When I saw the title and description I tried debugging your code with the expectation that you were trying to build up arguments for patch() calls.

Image Analyst

Image Analyst (view profile)

on 7 Jan 2013

A patch puts up a graphic area in the overlay above your image. Do you want that? I thought you wanted grid lines burned into your image, like I showed you. If you want grid lines in the overlay above the image, you can easily to that by adapting my code to use the line() or plot() function.

Walter Roberson

Walter Roberson (view profile)

on 7 Jan 2013

The original code expands the matrix so that all of the original data is still there, but with grid lines inserted. Except, that is, that the original code only looks at the first color plane and needs to be amended to work with all 3 color planes.

Image Analyst

Image Analyst (view profile)

on 7 Jan 2013

Ah, I see. I also got thrown by the "patch" terminology.