필터 지우기
필터 지우기

Loading several images in a loop makes all the images the same

조회 수: 1 (최근 30일)
saad khalid
saad khalid 2015년 8월 3일
답변: Image Analyst 2015년 8월 3일
The program I'm working on takes values for intensity, either positive or negative, or brain function, and then maps specific parts of the brain according to that intensity(blue for negative, red for positive). I want to create 18 different images, as I have data from 18 different activities. So, I'm running my code in a loop 18 times, which should create 18 images, each with different parts colored differently. The loop runs quickly, and while I watch it run, I see all the different colors appear. However, by the end of the loop, it has made every single image the exact same. All the images look the same as the first one. However, if I run my code without the loop, just making it load figures individually, they all appear different, as they should. I don't exactly know what's going wrong when I run the loop.
Data = xlsread('ResultsTable1.xlsx', 2, 'B2:H19')
[dataLabelsA,dataLabelsB,dataLabelsC] = xlsread('ResultsTable1.xlsx', 2, 'A2:A19')
brain = imread('brain image', 'png');
[central,centralMap,centralAlpha] = imread('central lobe filled .png');
[cerebellum,cerebellumMap,cerebellumAlpha] = imread('cerebellum filled .png');
[cingulate,cingulateMap,cingulateAlpha] = imread('cingulate lobe filled .png');
[frontal, frontalMap, frontalAlpha] = imread('frontal lobe filled .png');
[occipital, occipitalMap, occipitalAlpha] = imread('occipital lobe filled .png');
[parietal, parietalMap, parietalAlpha] = imread('parietal lobe filled .png');
colors = [];
for i = 1:18
for j = 1:6;
if Data(i,j) > 0;
colors(j,:,i) = [(Data(i,j)/.8)*255 0 0];
end
if Data(i,j) < 0;
colors(j,:,i) = [0 0 (Data(i,j)/(-.55))*255];
end
if isnan(Data(i,j)) == 1
colors(j,:,i) = [255 255 255];
end
end
%central lobe color control
redChannelCentral = central(:, :, 1);
greenChannelCentral = central(:, :, 2);
blueChannelCentral = central(:, :, 3);
% Find pixels that are pure black - black in all 3 channels.
blackPixelsCentral = redChannelCentral == 0 & greenChannelCentral == 0 & blueChannelCentral == 0;
% Make red (255,0,0)
redChannelCentral(blackPixelsCentral) = colors(2,1,i);
greenChannelCentral(blackPixelsCentral) = colors(2,2,i);
blueChannelCentral(blackPixelsCentral) = colors(2,3,i);
% Recombine separate color channels into a single, true color RGB image.
central = cat(3, redChannelCentral, greenChannelCentral, blueChannelCentral);
%cerebellum color control
redChannelCerebellum = cerebellum(:, :, 1);
greenChannelCerebellum = cerebellum(:, :, 2);
blueChannelCerebellum = cerebellum(:, :, 3);
% Find pixels that are pure black - black in all 3 channels.
blackPixelsCerebellum = redChannelCerebellum == 0 & greenChannelCerebellum == 0 & blueChannelCerebellum == 0;
% Make red (255,0,0)
redChannelCerebellum(blackPixelsCerebellum) = colors(6,1,i);
greenChannelCerebellum(blackPixelsCerebellum) = colors(6,2,i);
blueChannelCerebellum(blackPixelsCerebellum) = colors(6,3,i);
% Recombine separate color channels into a single, true color RGB image.
cerebellum = cat(3, redChannelCerebellum, greenChannelCerebellum, blueChannelCerebellum);
%cingulate color control
redChannelCingulate = cingulate(:, :, 1);
greenChannelCingulate = cingulate(:, :, 2);
blueChannelCingulate = cingulate(:, :, 3);
% Find pixels that are pure black - black in all 3 channels.
blackPixelsCingulate = redChannelCingulate == 0 & greenChannelCingulate == 0 & blueChannelCingulate == 0;
% Make red (255,0,0)
redChannelCingulate(blackPixelsCingulate) = colors(5,1,i);
greenChannelCingulate(blackPixelsCingulate) = colors(5,2,i);
blueChannelCingulate(blackPixelsCingulate) = colors(5,3,i);
% Recombine separate color channels into a single, true color RGB image.
cingulate = cat(3, redChannelCingulate, greenChannelCingulate, blueChannelCingulate);
%frontal color control
redChannelFrontal = frontal(:, :, 1);
greenChannelFrontal = frontal(:, :, 2);
blueChannelFrontal = frontal(:, :, 3);
% Find pixels that are pure black - black in all 3 channels.
blackPixelsFrontal = redChannelFrontal == 0 & greenChannelFrontal == 0 & blueChannelFrontal == 0;
% Make red (255,0,0)
redChannelFrontal(blackPixelsFrontal) = colors(3,1,i);
greenChannelFrontal(blackPixelsFrontal) = colors(3,2,i);
blueChannelFrontal(blackPixelsFrontal) = colors(3,3,i);
% Recombine separate color channels into a single, true color RGB image.
frontal = cat(3, redChannelFrontal, greenChannelFrontal, blueChannelFrontal);
%occipital color control
redChannelOccipital = occipital(:, :, 1);
greenChannelOccipital = occipital(:, :, 2);
blueChannelOccipital = occipital(:, :, 3);
% Find pixels that are pure black - black in all 3 channels.
blackPixelsOccipital = redChannelOccipital == 0 & greenChannelOccipital == 0 & blueChannelOccipital == 0;
% Make red (255,0,0)
redChannelOccipital(blackPixelsOccipital) = colors(4,1,i);
greenChannelOccipital(blackPixelsOccipital) = colors(4,2,i);
blueChannelOccipital(blackPixelsOccipital) = colors(4,3,i);
% Recombine separate color channels into a single, true color RGB image.
occipital = cat(3, redChannelOccipital, greenChannelOccipital, blueChannelOccipital);
%parietal color control
redChannelParietal = parietal(:, :, 1);
greenChannelParietal = parietal(:, :, 2);
blueChannelParietal = parietal(:, :, 3);
% Find pixels that are pure black - black in all 3 channels.
blackPixelsParietal = redChannelParietal == 0 & greenChannelParietal == 0 & blueChannelParietal == 0;
% Make red (255,0,0)
redChannelParietal(blackPixelsParietal) = colors(1,1,i);
greenChannelParietal(blackPixelsParietal) = colors(1,2,i);
blueChannelParietal(blackPixelsParietal) = colors(1,3,i);
% Recombine separate color channels into a single, true color RGB image.
parietal = cat(3, redChannelParietal, greenChannelParietal, blueChannelParietal);
figure(i)
I = imshow(brain);
hold on
centralShow = imshow(central);
set(centralShow,'AlphaData',centralAlpha);
cerebellumShow = imshow(cerebellum);
set(cerebellumShow, 'AlphaData', cerebellumAlpha);
cingulateShow = imshow(cingulate);
set(cingulateShow, 'AlphaData', cingulateAlpha);
frontalShow = imshow(frontal);
set(frontalShow, 'AlphaData', frontalAlpha);
occipitalShow = imshow(occipital);
set(occipitalShow, 'AlphaData', occipitalAlpha);
parietalShow = imshow(parietal);
set(parietalShow, 'AlphaData', parietalAlpha);
title(dataLabelsC(i))
end

답변 (1개)

Image Analyst
Image Analyst 2015년 8월 3일
We don't have your data. Your best best is to use the debugger to figure out when, where, and why your displayed images all become the same.

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by