I want to code an image stabilizer but when I try to copy an RGB image to a m*n*3 zeros matrix and display it I get a weird result.

2 views (last 30 days)
My problem is in the "% Corrected image" section, here is my code (I attached the images I'm using):
close all; clear all; clc;
% Image loading
imds = imageDatastore('blablabla');
images = readall(imds);
% Centroid calculation
centroid1 = centroidFinder(images{1});
centroid2 = centroidFinder(images{2});
% Centroid difference
centroidD = centroid2-centroid1;
% Corrected image
sizeI = size(images{1});
correctedDim1 = sizeI(1)+abs(centroidD(1)); correctedDim2 = sizeI(2)+abs(centroidD(2)); correctedDim3 = sizeI(3);
correctedI = zeros(correctedDim1, correctedDim2, correctedDim3);
correctedI(1:1:sizeI(1), 1:1:sizeI(2), :) = images{2};
function centroid = centroidFinder(image)
%{ NOTE: Add a range for G2 %}
[R,G,B] = imsplit(image); % RGB image split
G2 = G == max(max(G)); % Discard of elements not belinging to G
C = regionprops(true(size(G2)),G2,'WeightedCentroid'); % Centroid calculation
centroid = [round(C.WeightedCentroid(1)), round(C.WeightedCentroid(2))]; % Centroid vector output

Accepted Answer

DGM on 10 Mar 2023
Since the expected scale of image data depends on the class of the array, you need to make sure that the array is the appropriate class for the data you're putting in it.
correctedI = zeros(correctedDim1, correctedDim2, correctedDim3, 'uint8');

More Answers (0)

Community Treasure Hunt

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

Start Hunting!