Prev: smooth3
Next: Lowpass Butterworth Filter
From: Matt on 29 Jan 2010 10:58 Steve Eddins <Steve.Eddins(a)mathworks.com> wrote in message <hjukmj$cug$1(a)fred.mathworks.com>... > > So I suspect that maybe something else in your code, rather than the > call to imwrite, is taking a minute. Can you post a specific code > fragment that takes a minute, and can you show us how you timed it? > > --- > Steve Eddins > http://blogs.mathworks.com/steve/ Steve -- thanks for the response. The code is shown below. Basically, I read in a text file containing the centers and radii of a bunch of circles. I populate an array with the circles, and then attempt to write it to disk. If I comment out the imwrite command, the code executes in less than a second. With the imwrite command, it takes maybe 30 seconds. fid = fopen([specimen,num2str(fnum(ii)),intype]); C = textscan(fid,'%f %f %f %f'); fclose(fid); M(:,1) = C{1}; M(:,2) = C{2}; M(:,3) = C{3}; C = 0; M = sortrows(M,2); [r c] = size(M); if (M(2,2)-M(1,2))>2*M(1,3) M = M(2:r,1:c); end [r c] = size(M); xmin = round(min(M(:,1)-M(:,3))*sf); xmax = round(max(M(:,1)+M(:,3))*sf); ymin = round(min(M(:,2)-M(:,3))*sf); ymax = round(max(M(:,2)+M(:,3))*sf); M(:,1) = round(M(:,1)*sf - xmin); M(:,2) = round(M(:,2)*sf - ymin); Rows = ymax-ymin; Cols = xmax-xmin; I = zeros(Rows,Cols); for jj = 1:r rad = M(jj,3); bot = ceil(Rows - M(jj,2) + rad); top = floor(Rows - M(jj,2) - rad)+1; left = floor(M(jj,1) - rad)+1; right = ceil(M(jj,1) + rad); for nn = top:bot for kk = left:right if ((kk - M(jj,1))^2+(nn-(Rows-M(jj,2)))^2)<=rad^2 I(nn,kk) = 1; end end end imwrite(I,[specimen,num2str(fnum(ii)),outtype],'png','bitdepth',1); end NOTE: in the above code, 'sf' is a scale factor to control image size. It is currently set to 1.
From: Matt on 29 Jan 2010 11:09 > for jj = 1:r > rad = M(jj,3); > bot = ceil(Rows - M(jj,2) + rad); > top = floor(Rows - M(jj,2) - rad)+1; > left = floor(M(jj,1) - rad)+1; > right = ceil(M(jj,1) + rad); > for nn = top:bot > for kk = left:right > if ((kk - M(jj,1))^2+(nn-(Rows-M(jj,2)))^2)<=rad^2 > I(nn,kk) = 1; > end > end > end > imwrite(I,[specimen,num2str(fnum(ii)),outtype],'png','bitdepth',1); > end OK, so I am an idiot. Look at the location of the imwrite command -- it's inside a loop. Thus, I am writing the image 'r' times (1633 in this case). That will teach me to code while drinking bourbon. Thanks to all for the help. Matt
From: Steve Eddins on 29 Jan 2010 11:29
Matt wrote: >> for jj = 1:r >> rad = M(jj,3); >> bot = ceil(Rows - M(jj,2) + rad); >> top = floor(Rows - M(jj,2) - rad)+1; >> left = floor(M(jj,1) - rad)+1; >> right = ceil(M(jj,1) + rad); >> for nn = top:bot >> for kk = left:right >> if ((kk - M(jj,1))^2+(nn-(Rows-M(jj,2)))^2)<=rad^2 >> I(nn,kk) = 1; >> end >> end >> end >> >> imwrite(I,[specimen,num2str(fnum(ii)),outtype],'png','bitdepth',1); >> end > > OK, so I am an idiot. Look at the location of the imwrite command -- > it's inside a loop. Thus, I am writing the image 'r' times (1633 in this > case). That will teach me to code while drinking bourbon. Don't worry, Matt. We've all been there! Many times. One of the most important steps in debugging or problem-solving in general is questioning and testing assumptions. That's what was behind my earlier post. You posted that imwrite was slow (it took a minute). In the spirit of questioning assumptions I said to myself, "Well, maybe imwrite is slow, but are we sure? How do we know? Maybe it's something else"? The other part of what happened to you is the "cardboard cut-out" phenomenon. It's very common that a programmer will find a bug simply by showing the code to someone else, even if that person never says a word. In a code review, for example, a programmer will be explaining their code and stop in mid-sentence, scratch their head for a minute, exclaim, "Oops! That line is wrong!" The code reviewer, who never said a word during the whole process, might as well have been a cardboard cut-out. In this case, you found your own bug simply by posting it and then reading your own post! Very common, even if there is no bourbon involved. --- Steve Eddins http://blogs.mathworks.com/steve/ |