My problem is when putting the pictures together my indices have to be an integer so how do I take a third of each picture and put them together?

2 views (last 30 days)
Hi, I'm trying to make a red filter by picking a value between 0 and 1 and dividing the remainder to the Green and Blue layers of the picture then stitching together a grayscale, the original, and the red filtered picture next to each other...
function [ outImg] = redFilter( inImg , redVal)
%red filter
[r,c,d] = size(inImg);
%grayscale
gray = 0.2989*inImg(:,:,1)+0.5870*inImg(:,:,2)+0.1140*inImg(:,:,3);
%red filter
GandB = 1 - redVal;
red = redVal*inImg(:,:,1)+(GandB/2)*inImg(:,:,2)+(GandB/2)*inImg(:,:,3);
%final image
gray3 = gray(r,round(c/3),d);
red3 = red(r,c/3,d);
inImg3 = InImg(r,c/3,d);
outImg = [gray3, inImg3, red3];
end
  2 Comments
Stephen23
Stephen23 on 5 Mar 2017
@Brady Barkemeyer: this time I formatted your code for you. Next time you can do it yourself: first select the code text, then click the Code {} button above the textbox.
Jan
Jan on 6 Mar 2017
The question is not clear to me. What does "take a third" exactly mean? Do you mean something like gray(r, 1:3:end, d)?

Sign in to comment.

Answers (1)

Sonam Gupta
Sonam Gupta on 8 Mar 2017
I assume that you are trying to stitch the images together such that for the same image 1/3rd is in grayscale, another 1/3rd is redFiltered and remaining 1/3rd is real image. Few points to pay attention are gray is a two dimensional matrix having same dimension as that of image size. By using the syntax 'gray(r,round(c/3),d)' you are referring to single element of the array. Moreover, the way gray is defined it does not have third dimension.
Given below is the modified version of your code but you still need to check what you want to achieve with gray.
function [ outImg] = redFilter_modified( inImg , redVal)
%red filter
[r,c,d] = size(inImg);
%grayscale
gray = zeros(r, c, d);
gray(:, : , 1) = 0.2989*inImg(:,:,1);
gray(:, :, 2) = 0.5870*inImg(:,:,2);
gray(:, :,3) = 0.1140*inImg(:,:,3);
%red filter
GandB = 1 - redVal;
red = zeros(r, c, d);
red(:, :, 1) = redVal*inImg(:,:,1);
red(:, :,2) = (GandB/2)*inImg(:,:,2);
red(:, :, 3) = (GandB/2)*inImg(:,:,3);
%final image
%creating the image array taking one third from all there matrices
width = round(c/3); % number of rows should be same, only number of columns will be divided by 3
gray3 = gray(:,1:width, :);
red3 = red(:,width+1: round(2*c/3), :);
inImg3 = inImg(:,round((2*c/3)+1 : c), :);
outImg = [gray3,red3, inImg3];
imshow(outImg);
end
Hope this answers your query.

Community Treasure Hunt

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

Start Hunting!