count number of pixels and write to a txt file [message #90144] |
Sun, 01 February 2015 22:38  |
beardown912
Messages: 2 Registered: February 2015
|
Junior Member |
|
|
Hello,
I've been banged my head and pulling out my hairs for last a couple of days, but I am still struggling. And I decided to get some help from you guys.
What I've been trying to is counting number of pixels meets a condition and write it into a text file with the file name. And I am dealing with about 20,000 image files.
I used 'where' function and it works fine in displaying while looping.
However, only the last value of pixel number and all file names are appended.
Below is what I've been reached. It looks very sloppy :)
------------------------------
pro filelist
filter = ['*.tif']
file = file_search(dialog_pickfile(PATH='C:\', filter=filter, get_path=outdir, /multiple_files), count=numbers)
FILE_MKDIR, outdir
FOR j=0, numbers-1 DO BEGIN
Image = read_tiff(file[j])
index = where(image ge 210, ct)
if ct GT 50000 then begin
openw, lun, outdir + 'test00.txt ', /get_lun
printf, lun, ct, file
close, lun
free_lun, lun
endif
ENDFOR
end
------------------------------------------------
Any tip would be greatly appreciated.
Thanks,
Kim
|
|
|
Re: count number of pixels and write to a txt file [message #90145 is a reply to message #90144] |
Sun, 01 February 2015 23:43   |
Moritz Fischer
Messages: 32 Registered: June 2013
|
Member |
|
|
Am 02.02.2015 um 07:38 schrieb beardown912@gmail.com:
> Hello,
>
> I've been banged my head and pulling out my hairs for last a couple
> of days, but I am still struggling. And I decided to get some help
> from you guys.
>
> What I've been trying to is counting number of pixels meets a
> condition and write it into a text file with the file name. And I am
> dealing with about 20,000 image files. I used 'where' function and it
> works fine in displaying while looping. However, only the last value
> of pixel number and all file names are appended. Below is what I've
> been reached. It looks very sloppy :)
>
> ------------------------------ pro filelist
>
> filter = ['*.tif'] file = file_search(dialog_pickfile(PATH='C:\',
> filter=filter, get_path=outdir, /multiple_files), count=numbers)
> FILE_MKDIR, outdir
>
> FOR j=0, numbers-1 DO BEGIN
>
> Image = read_tiff(file[j])
>
> index = where(image ge 210, ct)
>
> if ct GT 50000 then begin openw, lun, outdir + 'test00.txt ',
> /get_lun printf, lun, ct, file close, lun free_lun, lun endif
>
> ENDFOR end ------------------------------------------------
>
> Any tip would be greatly appreciated.
>
> Thanks, Kim
>
Hi!
You are re-opening (and thus overwriting) the file. You could
a) add "/APPEND" to the openw command
b) pull the openw/close/free_lun out of the loop.
Solution b) is faster, but considering the WHERE command it probably
doesn't matter in this case.
I also think you can ommit either the free_lun or the close command, but
I'm not sure at the moment.
m
|
|
|
Re: count number of pixels and write to a txt file [message #90147 is a reply to message #90144] |
Mon, 02 February 2015 04:31   |
Yngvar Larsen
Messages: 134 Registered: January 2010
|
Senior Member |
|
|
pro myfilecounts, files, outdir
openw, lun, outdir + 'filecounts.txt ', /get_lun
foreach file, files do begin
index = where(read_tiff(file) ge 210, ct)
if ct gt 50000 then printf, lun, ct, ' ', file_basename(file)
endforeach
free_lun, lun
end
On Monday, 2 February 2015 07:38:35 UTC+1, beard...@gmail.com wrote:
> Hello,
>
> I've been banged my head and pulling out my hairs for last a couple of days, but I am still struggling. And I decided to get some help from you guys.
>
> What I've been trying to is counting number of pixels meets a condition and write it into a text file with the file name. And I am dealing with about 20,000 image files.
> I used 'where' function and it works fine in displaying while looping.
> However, only the last value of pixel number and all file names are appended.
> Below is what I've been reached. It looks very sloppy :)
>
> ------------------------------
> pro filelist
>
> filter = ['*.tif']
> file = file_search(dialog_pickfile(PATH='C:\', filter=filter, get_path=outdir, /multiple_files), count=numbers)
> FILE_MKDIR, outdir
>
> FOR j=0, numbers-1 DO BEGIN
>
> Image = read_tiff(file[j])
>
> index = where(image ge 210, ct)
>
> if ct GT 50000 then begin
> openw, lun, outdir + 'test00.txt ', /get_lun
> printf, lun, ct, file
> close, lun
> free_lun, lun
> endif
>
> ENDFOR
> end
> ------------------------------------------------
>
> Any tip would be greatly appreciated.
>
> Thanks,
> Kim
|
|
|
Re: count number of pixels and write to a txt file [message #90148 is a reply to message #90144] |
Mon, 02 February 2015 05:18  |
beardown912
Messages: 2 Registered: February 2015
|
Junior Member |
|
|
On Monday, February 2, 2015 at 1:38:35 AM UTC-5, beard...@gmail.com wrote:
> Hello,
>
> I've been banged my head and pulling out my hairs for last a couple of days, but I am still struggling. And I decided to get some help from you guys.
>
> What I've been trying to is counting number of pixels meets a condition and write it into a text file with the file name. And I am dealing with about 20,000 image files.
> I used 'where' function and it works fine in displaying while looping.
> However, only the last value of pixel number and all file names are appended.
> Below is what I've been reached. It looks very sloppy :)
>
> ------------------------------
> pro filelist
>
> filter = ['*.tif']
> file = file_search(dialog_pickfile(PATH='C:\', filter=filter, get_path=outdir, /multiple_files), count=numbers)
> FILE_MKDIR, outdir
>
> FOR j=0, numbers-1 DO BEGIN
>
> Image = read_tiff(file[j])
>
> index = where(image ge 210, ct)
>
> if ct GT 50000 then begin
> openw, lun, outdir + 'test00.txt ', /get_lun
> printf, lun, ct, file
> close, lun
> free_lun, lun
> endif
>
> ENDFOR
> end
> ------------------------------------------------
>
> Any tip would be greatly appreciated.
>
> Thanks,
> Kim
Thank you Moritz & Yngvar, I really appreciate your times and answers.
Your suggestion and the code snippet work perfectly.
I can go a step further and keep up learning.
Kim
|
|
|