Error with matrix dimensions

조회 수: 1 (최근 30일)
Srdjan
Srdjan 2014년 11월 18일
댓글: Srdjan 2014년 11월 18일
Hi, I'm making a code for scene cut detection, but I always get an error for line 14: matrix dimensions must agree... How do I fix this? Thank you.
THRESH = 0.5;
video = mmreader('rhinos.avi');
numFrames = video.NumberOfFrames;
prevHist = zeros(256*3,1);
diffHist= zeros(numFrames,1);
cutInd = 1;
for i = 1 : numFrames
currFrame = read(video, i);
currHistR = imhist(currFrame(:, :, 1));
currHistG = imhist(currFrame(:, :, 2));
currHistB = imhist(currFrame(:, :, 3));
currHist = [currHistR currHistG currHistB];
whos prevHist currHist;
diffHist(i) = sqrt(sum((currHist - prevHist).^2));
if (diffHist(i) > THRESH)
cutFrames(:, :, :, cutInd) = currFrame(:, :, :);
cutInd = cutInd + 1;
end
prevHist = currHist;
end
stem(diffHist);
figure;
montage(cutFrames);
  댓글 수: 4
Andrew Reibold
Andrew Reibold 2014년 11월 18일
편집: Andrew Reibold 2014년 11월 18일
You will need to make them both the same size to use the subtraction operation in line 14 on them.
What code are you using to make the 768x1 into a 256x3? (What code are you using giving you the A(I)=B error. That should be easy to troubleshoot hopefully) I assume you are just trying to split it into 3 parts?
There are more efficient ways to make a new matrix, but have you tried something like this?
prevHist2(:,1) = prevHist(1:256)
prevHist2(:,2) = prevHist(257:512)
prevHist2(:,3) = prevHist(513:768)
and then using the new version of prevHist that is now rearranged?
Andrew Reibold
Andrew Reibold 2014년 11월 18일
Nevermind, I see you have a solution! Best wishes

댓글을 달려면 로그인하십시오.

채택된 답변

MA
MA 2014년 11월 18일
you have two mistake:
prevHist = zeros(256*3,1)
and
diffHist(i) = sqrt(sum((currHist - prevHist).^2))
your correct code should be:
clear all
close all
clc;
THRESH = 0.5;
video = mmreader('rhinos.avi');
numFrames = video.NumberOfFrames;
prevHist = zeros(256,3);
diffHist= zeros(numFrames,1);
cutInd = 1;
for i = 1 : numFrames
currFrame = read(video, i);
currHistR = imhist(currFrame(:, :, 1));
currHistG = imhist(currFrame(:, :, 2));
currHistB = imhist(currFrame(:, :, 3));
currHist = [currHistR currHistG currHistB];
whos prevHist currHist;
diffHist(i) = sqrt(sum((currHist(i) - prevHist(i)).^2));
if (diffHist(i) > THRESH)
cutFrames(:, :, :, cutInd) = currFrame(:, :, :);
cutInd = cutInd + 1;
end
prevHist = currHist;
end
stem(diffHist);
figure;
montage(cutFrames);
  댓글 수: 1
Srdjan
Srdjan 2014년 11월 18일
Thx mate, it works fine. :)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

MA
MA 2014년 11월 18일
you have two mistake:
prevHist = zeros(256*3,1)
and
diffHist(i) = sqrt(sum((currHist - prevHist).^2))
your correct code should be:
clear all
close all
clc;
THRESH = 0.5;
video = mmreader('rhinos.avi');
numFrames = video.NumberOfFrames;
prevHist = zeros(256,3);
diffHist= zeros(numFrames,1);
cutInd = 1;
for i = 1 : numFrames
currFrame = read(video, i);
currHistR = imhist(currFrame(:, :, 1));
currHistG = imhist(currFrame(:, :, 2));
currHistB = imhist(currFrame(:, :, 3));
currHist = [currHistR currHistG currHistB];
whos prevHist currHist;
diffHist(i) = sqrt(sum((currHist(i) - prevHist(i)).^2));
if (diffHist(i) > THRESH)
cutFrames(:, :, :, cutInd) = currFrame(:, :, :);
cutInd = cutInd + 1;
end
prevHist = currHist;
end
stem(diffHist);
figure;
montage(cutFrames);

Kevin Claytor
Kevin Claytor 2014년 11월 18일
Let's take a look at the definition of;
diffHist= zeros(numFrames,1);
This is a [numFrames x 1] size array. Into this you're trying to put;
sqrt(sum((currHist - prevHist).^2));
So, first in regards to the sizes, you can only subtract them if they're the same size;
>> size(currHist)
>> 256x3
>> size(prevHist)
>> 768x1
Which you seem to have resolved in your comments. But now you're trying to put a 256x3 array (sqrt(...)) into a 1x1 slot (diffHist(i)). Either you want the entire array;
diffHist = zeros(256, 3, numFrames);
diffHist(:, :, i) = sqrt(...)
Or you just want some representative value of the difference histogram;
temp_dist_hist = sqrt(...);
representative_value = sum(temp_dist_hist(:)); % Just an example, depends on what you're trying to do
diffHist(i) = representative_value;
Hope, this helps.
  댓글 수: 1
Srdjan
Srdjan 2014년 11월 18일
Your example helped a lot, thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by