Non-singleton dimensions of the two input arrays must match each other.

1 visualizzazione (ultimi 30 giorni)
I cannot understand what's the problem. This code works for other jpg images but for my dicom stack of images it comes up with an error (see subject line), to the _Background_blending = bsxfun(@times, Background, bsxfun(@minus,1,alpha)); line. PLease Help. Thanks!
Foreground = im2double(mask)*350-50;
Background = im2double(I);
% build alpha layer for Foreground
alpha = bsxfun(@times, ones(size(Foreground,1), size(Foreground,2)), .6);
figure(3)
imshow(alpha)
% find a scale dynamically with some limit
Foreground_min = min( min(Foreground(:)), -50 );
Foreground_max = max( max(Foreground(:)), 300 );
% overlay the image by blending
Background_blending = bsxfun(@times, Background, bsxfun(@minus,1,alpha));

Risposta accettata

Walter Roberson
Walter Roberson il 27 Mar 2018
Your code overuses bsxfun. You only need at most one bsxfun call.
Your code probably reduces down to
Background_blending = Background .* (1-0.6);
Anyhow, your bug is in assuming that mask and "I" have the same number of rows and columns (but possibly different numbers of planes).
  4 Commenti
Walter Roberson
Walter Roberson il 27 Mar 2018
Yes, what I said about mask and "I" having different numbers of rows and columns really is my answer to your earlier problem. The fact that it worked on some jpeg images does not tell us anything useful, because we do not know that the jpeg images had the same number of rows and columns as the dicom images, and we have no information about the size of mask or the size of "I". If you had posted more complete code we might have been able to tell you why the sizes were not the same.
Stelios Fanourakis
Stelios Fanourakis il 28 Mar 2018
Here is the entire code. I still get the same error at out = Background_blending + Foreground_blending; ''Array dimensions must match for binary array op''
function [BW,maskedImage] = segmentImage(RGB,~,~) %segmentImage Segment image using auto-generated code from imageSegmenter App % [BW,MASKEDIMAGE] = segmentImage(RGB) segments image RGB using % auto-generated code from the imageSegmenter App. The final segmentation % is returned in BW, and a masked image is returned in MASKEDIMAGE.
% Auto-generated by imageSegmenter app on 12-Mar-2018 %----------------------------------------------------
% Convert RGB image into L*a*b* color space.
X= RGB;
% Create empty mask. BW = false(size(X,1),size(X,2));
% Draw Rectangle xPos = [1.3746 881.3172 881.3172 1.3746]; yPos = [0.0412 0.0412 173.4606 173.4606]; m = size(BW, 1); n = size(BW, 2); addedRegion = poly2mask(xPos, yPos, m, n); BW = BW | addedRegion;
% Draw Freehand xPos = [1.3746]; yPos = [170.7079]; m = size(BW, 1); n = size(BW, 2); addedRegion = poly2mask(xPos, yPos, m, n); BW = BW | addedRegion;
% Draw Polygon xPos = [1.3746 5.0448 354.6362]; yPos = [173.4606 263.3817 168.8728]; m = size(BW, 1); n = size(BW, 2); addedRegion = poly2mask(xPos, yPos, m, n); BW = BW | addedRegion;
% Draw Polygon xPos = [531.7258 812.5000 835.4391 851.0376 854.7079 856.5430]; yPos = [170.7079 240.4427 256.0412 256.0412 256.0412 146.8513]; m = size(BW, 1); n = size(BW, 2); addedRegion = poly2mask(xPos, yPos, m, n); BW = BW | addedRegion;
% Dilate mask with disk radius = 1; decomposition = 0; se = strel('disk', radius, decomposition); BW = imdilate(BW, se);
% Fill holes BW = imfill(BW, 'holes');
% Create masked image.
for i = 1:length(theFiles) baseFileName = theFiles(i).name; fullFileName = fullfile(img_dir, baseFileName); fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
I = dicomread(fullFileName);
mask =zeros(size(BW)); mask(10:end-10,10:end-10) = 1;
Foreground = double(mask); Background = double(I);
% build alpha layer for Foreground alpha = bsxfun(@times, ones(size(Foreground,1), size(Foreground,2)), .6); figure(3) imshow(alpha)
% find a scale dynamically with some limit Foreground_min = min( min(Foreground(:)), -50 ); Foreground_max = max( max(Foreground(:)), 300 );
% overlay the image by blending Background_blending = Background.*(1-0.6);
%Background_blending = Background.*repmat((1-alpha), 1, 1, 3);
Foreground_blending = (Foreground-Foreground_min) /(Foreground_max-Foreground_min).*(alpha);
%out = bsxfun(@plus, Background_blending, Foreground_blending); out = Background_blending + Foreground_blending;
figure(4) imshow(out)
maskedImage = I; imshow(maskedImage);
S = mask.*repmat(BW,[1,1,3]);
imshow(S) drawnow;
end

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by