Is there any way to make this code better?

Hi, I have this code that draws a rectangle onto an image. However, it is very slow for if I wish to draw multiple rectangles. Is there any better way to draw something onto an image?
function imret = image_dsquare( im, r1, c1, r2, c2, val )
[imr imc] = size(im);
imret = im;
for r = r1:r2
for c = c1:c2
if r >= 1 && r <= imr && c >= 1 && c <= imc
imret(r, c) = val;
end
end
end
end

 採用された回答

Sean de Wolski
Sean de Wolski 2012 年 7 月 17 日

1 投票

imret(max(r1,1):min(r2,imr),max(c1,1):min(c2,imc)) = val;

3 件のコメント

Qingyang
Qingyang 2012 年 7 月 18 日
Thanks, that did help a lot. But, is there any better (even faster) way of drawing points on an image?
Sean de Wolski
Sean de Wolski 2012 年 7 月 18 日
That is probably as fast as it's going to get for the code you have above...
Why is speed so important? How many time are you actually going to call this? Does it matter if one approach takes a few ten thousanths of a second longer?
Qingyang
Qingyang 2012 年 7 月 18 日
Thanks for your help. I was calling this maybe >1000 times for each run. However, I did come up with another way to draw on the image that was much faster.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by