好的,這是一個計算機圖形學問題。我正在學習計算機圖形學,也不擅長 Matlab。當我嘗試實作邊界填充演算法時,我發現它不起作用。有偽代碼:
void BoundaryFill(int x, int y, COLORREF boundaryValue, COLORREF newValue)
{
if(GetPixel(x,y) != boundaryValue&&
GetPixel(x,y) != newValue)// if pixel not already filled and not reach to the
boundary then
{
SetPixel(x,y,newValue);//fill the pixel
BoudaryFill(x,y-1,boudaryValue,newValue);
BoudaryFill(x,y 1,boudaryValue,newValue);
BoudaryFill(x-1,y,boudaryValue,newValue);
BoudaryFill(x 1,y,boudaryValue,newValue);
}
}
好的,不難理解。但是當我用matlab寫的時候,有一個錯誤。首先,我定義了一個“img”:img = one(600,800,3); 可以理解為畫布,其實它是一個矩陣,第三個引數可以是RGB的矩陣,像這樣:img(x,y,[0,0,0]/255); 無論如何,我可以使用 imshow(img) 將畫布顯示為圖形,并且可以更改每個像素的顏色。我可以畫一個封閉的圖形并填充它。我們看一下matlab代碼:
%This is the script
clc;
img = ones(600,800,3);
img = DDA_line(img,3,3,3,50,[0,0,0]/255);
img = DDA_line(img,30,3,30,50,[0,0,0]/255);
img = DDA_line(img,3,3,30,3,[0,0,0]/255);
img = DDA_line(img,3,50,30,50,[0,0,0]/255);
img = Boundaryfill(img,15,26,[0,0,0]/255,[255,0,0]/255);
imshow(img)
% This is DDA_line
function img = DDA_line(img, x1, y1, x2, y2, color)
e = max(abs(x2-x1), abs(y2-y1));
dx = (x2-x1)/e;
dy = (y2-y1)/e;
img(x1,y1,:)=color;
for i = 0:e
x1 = x1 dx;
y1 = y1 dy;
img(round(x1),round(y1),:)=color;
end
%This is the Boudaryfill function
function img = Boundaryfill(img, x, y, boundaryvalue, new)
if img(x,y,:) ~= boundaryvalue && img(x,y,:) ~= new
img(x,y,:) = new;
img=Boundaryfill(img,x,y-1,boundaryvalue, new);
img=Boundaryfill(img,x,y 1,boundaryvalue, new);
img=Boundaryfill(img,x-1,y,boundaryvalue, new);
img=Boundaryfill(img,x 1,y,boundaryvalue, new);
end
end
它并不復雜,但是當我嘗試運行它時,它會發布一個錯誤。有資料:
| | the operands and && operator must be able to convert logical scalar value.
這似乎是一個語法錯誤,但我不知道如何解決它。這可以是第一個問題。我嘗試使用嵌套結構來表示相同的邏輯,但封閉的圖形沒有填充。可惜不能插入圖片。但是當我寫出這樣的句子時
if img(x,y,:) ~= boundaryvalue
雖然它不完整,但它填充了一個矩形。但是當我使用這個不完整的函式來填充一個三角形時,它失敗并出現錯誤。有資料:
Insufficient memory. The possible reason is that there is infinite recursion in the program.
我整天都在努力解決這個問題,但沒有成功,我真誠地希望你能給我你的建議。謝謝!
uj5u.com熱心網友回復:
注意要比較的矩陣的尺寸和方向。
這應該有助于:
function img = Boundaryfill(img, x, y, boundaryvalue, new)
temp = squeeze(img(x,y,:))';
if temp ~= boundaryvalue & (temp ~= new)
img(x,y,:) = new;
img=Boundaryfill(img,x,y-1,boundaryvalue, new);
img=Boundaryfill(img,x,y 1,boundaryvalue, new);
img=Boundaryfill(img,x-1,y,boundaryvalue, new);
img=Boundaryfill(img,x 1,y,boundaryvalue, new);
end
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/459011.html
上一篇:繪制3d函式失敗
