color image into blocks

2 views (last 30 days)
Elysi Cochin
Elysi Cochin on 7 Jan 2013
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 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.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst 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);
  3 Comments
Image Analyst
Image Analyst on 7 Jan 2013
Edited: Image Analyst 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
rgbImage = imread(fullFileName);
% 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
Elysi Cochin on 8 Jan 2013
thank u sir...

Sign in to comment.

More Answers (2)

Gamil Qaid
Gamil Qaid 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');

Walter Roberson
Walter Roberson 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.
  8 Comments
Walter Roberson
Walter Roberson 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 on 7 Jan 2013
Ah, I see. I also got thrown by the "patch" terminology.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!