Contrast Adjustment to an Image-Array indices must be positive integers or logical values error

1 visualizzazione (ultimi 30 giorni)
I'm trying to loop through an image, apply a transformation to each pixel, and map it back to its place in a new array but keep getting the error.
image = 'Barbara.tif'
%read image in workspace
I = imread(image);
imshow(I)
s = 47.2055;%std of the image
avg = 112.4668; %avg of all intensities in image
sN = 60; %new std
avgN = 120; %new mean
linI = I(:,:,1); %image
linZ =zeros(size(linI)); %allocate space for contrasted image
linS = sN/s; %calculate this once, then apply to each pixel
linAvg = (avgN-avg*linS); %calculate this once and apply to all
for i = 1:size(linI, 1)
for j = 1:size(linI,2)
pixel = linI(i,j);
new_pixel = pixel(linS) + linAvg;
if new_pixel < 0
new_pixel = pixel;
end
linZ(i,j) = new_pixel;
end
end
figure()
subplot(1,2,1)
imshow(I,[])
title('original image')
subplot(1,2,2)
imshow(linZ,[])
title('contrast image')

Risposte (1)

Image Analyst
Image Analyst il 23 Set 2022
Modificato: Image Analyst il 23 Set 2022
Tiffany, there are lots of errors in that. I've fixed most of them but I'm not sure what you're doing in one part. It doesn't make sense. Are you trying to do a Wallis filter? This is a sliding window filter where it makes the values inside the window match a specified mean and standard deviation. I'm attaching a demo for that. Or you might try to use a function for histogram matching like imhistmatch or my File Exchange https://www.mathworks.com/matlabcentral/fileexchange/28972-custom-shaped-histogram?s_tid=srchtitle
Here is a start at fixing your code.
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
markerSize = 40;
fprintf('Beginning to run %s.m ...\n', mfilename);
Beginning to run LiveEditorEvaluationHelperEeditorId.m ...
fileName = 'peppers.png'
fileName = 'peppers.png'
%read image in workspace
rgbImage = imread(fileName);
% Display images.
subplot(2,2,1)
imshow(rgbImage,[])
[rows, columns, numberOfColorChannels] = size(rgbImage)
rows = 384
columns = 512
numberOfColorChannels = 3
grayImage = rgbImage(:,:,1); % Extract only the red channel of the image
title('Original image')
subplot(2, 2, 2);
imshow(grayImage)
title('Input Gray Image')
% Compute original statistics.
originalStDev = std(double(grayImage(:))) % Actual std dev of the original image
originalStDev = 66.5441334903169
originalMean = mean2(grayImage) %avg of all intensities in image
originalMean = 120.780802408854
% Assign desired statistics of output image.
desiredStDev = 60; % Desired new std
desiredMean = 120; % Desired new mean
% Declare variables. Preallocate to speed up computations.
outputImage = zeros(size(grayImage)); % allocate space for contrast enhanced image
ratioOfStdDevs = desiredStDev / originalStDev % Calculate this once, then apply to each pixel
ratioOfStdDevs = 0.901657243891092
linAvg = (desiredMean-originalMean*ratioOfStdDevs) % Calculate this once and apply to all
linAvg = 11.097114585078
% Scale the image in a vectorized fashion
outputImage = ratioOfStdDevs * (double(grayImage) - originalMean) + desiredMean;
% Scale the image in a non-vectorized, loop fashion
% for row = 1 : rows
% for col = 1 : columns
% pixel = grayImage(row, col);
%
% %---------------------------------------------------------------
% new_pixel = pixel * ratioOfStdDevs + linAvg;
% %---------------------------------------------------------------
%
% if new_pixel < 0
% new_pixel = pixel;
% end
% outputImage(row, col) = new_pixel;
% end
% end
% Compute output statistics. See if we got what we wanted.
outputStDev = std(double(outputImage(:))) % Actual std dev of the output image
outputStDev = 59.9999999999999
outputMean = mean2(outputImage) % Mean of all intensities in the output image
outputMean = 120
subplot(2,2,3)
imshow(outputImage,[])
title('Contrast image')
  3 Commenti
Image Analyst
Image Analyst il 27 Set 2022
You're not really doing a Wallis filter because your code is not locally adaptive. See my attached Wallis filter demo.

Accedi per commentare.

Categorie

Scopri di più su Image Processing and Computer Vision in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by