Page 1 of 1
outputting pixel RGB values to CSV file
Posted: 2018-08-29T16:52:54-07:00
by gimpeme
What I'm trying to accomplish: I have a 10MP image and I want to identify all of the pixels it contains with RGB values larger than 50 (i.e. either R>50 or G>50 or B>50).
How I've though of going about it is to first identify RGB values for all of the pixels and then go through with a tool like MATLAB and pick out the ones that satisfy my criteria. I've found two commands to help with my first step:
- convert test.png output.txt >>> This does exactly what I want, except that (1) it doesn't save the output in a format readable by a program such as MATLAB, and (2) is a bit slow for a large image.
- convert test.png sparse-color: >>> This outputs exactly the information I need (one above provides unnecessary information beyond pixel location and RGB values), except that I can't find a way to generate an output file.
So I have a couple of questions:
- As far as my goal goes, is there any simpler way of accomplishing this?
- If I want to use the "sparse-color", can I get this output into a file?
- If I want to use either of the two, can I have it formatted as a CSV that other programs may more easily use?
Thanks in advance.
edit: I'm on ImageMagick 7.0.8-10 Q16 x64, Windows 10.
Re: outputting pixel RGB values to CSV file
Posted: 2018-08-29T17:34:23-07:00
by fmw42
This should work. My pipes to tr and sed are Unix commands to replace spaces with new line, remove the srgb, and then remove the parens. (Brute force method, but easier to understand than combining into one complex sed)
The example below shows the top left 3x3 area of the rose: Imagemagick special image. Replace rose: with your image.suffix.
Code: Select all
echo "X,Y,R,G,B" > file.csv
convert rose:[3x3+0+0] sparse-color: | tr " " "\n" | sed 's/srgb//g' | sed 's/[()]//g' >> file.csv
http://www.fmwconcepts.com/misc_tests/s ... v/file.csv
Re: outputting pixel RGB values to CSV file
Posted: 2018-08-29T18:11:27-07:00
by gimpeme
@fmw42: I understand some of those words haha. My knowledge of this kind of code is relatively limited.
I tried running that code you listed both in the cmd prompt and in a batch file, but neither worked. I got an error that "tr" is not recognized as an internal or external command.
I can't find any information on this error in the context of IM.
Also, I edited this into my first post, I'm on ImageMagick 7.0.8-10 Q16 x64, Windows 10, if that makes a difference.
Re: outputting pixel RGB values to CSV file
Posted: 2018-08-29T18:53:05-07:00
by fmw42
As I mentioned, the tr and sed are Unix commands. I am not sure they work in Windows without installed Windows equivalents. But on Windows 10, there is a built in Unix system. You can use that to run my command. Sorry, but I am not a Windows user and do not know the equivalent code to replace tr and sed.
I do not know if this will help, but doing a Google search for tr lists:
https://stackoverflow.com/questions/292 ... in-windows
Also see a list of Windows versions of some unix tools at:
http://gnuwin32.sourceforge.net/packages/coreutils.htm
For sed, see
http://gnuwin32.sourceforge.net/packages/sed.htm
P.S. If on IM 7, then change convert to magick.
Re: outputting pixel RGB values to CSV file
Posted: 2018-08-30T01:30:58-07:00
by snibgo
gimpeme wrote:What I'm trying to accomplish: I have a 10MP image and I want to identify all of the pixels it contains with RGB values larger than 50 (i.e. either R>50 or G>50 or B>50).
... As far as my goal goes, is there any simpler way of accomplishing this?
I assume you mean 50 on a scale of 0 to 255. Note that 50/255 * 100% = approx 19.6078%.
If you merely want to identify those pixels, you can do that in a single "magick" command, for example:
Code: Select all
magick toes.png ( +clone -channel RGB -threshold 19.6078% +channel -grayscale Brightness ) -alpha off -compose CopyOpacity -composite out.png
This makes transparent all pixels that have all RGB channels less than the threshold. If for some reason you want those values as text then instead of "out.png", write to "sparse-color:out.txt".
PS. The commands Fred showed work perfectly on my Windows 8.1 laptop, using tr and sed from Cygwin.
Re: outputting pixel RGB values to CSV file
Posted: 2018-08-30T09:10:27-07:00
by fmw42
Thanks for the filtering code, snibgo. I forgot to add code to do the filtering. My code only creates the csv file.
Re: outputting pixel RGB values to CSV file
Posted: 2018-08-30T09:45:49-07:00
by snibgo
As desired, the filtering can be done with the IM command, or externally in Mathlab or whatever is used. For efficiency I usually prefer to reduce data as early as I can.
I think Mathlab can directly read some image formats, so that might be a preferred method.
Re: outputting pixel RGB values to CSV file
Posted: 2018-08-30T10:11:43-07:00
by fmw42
Since you are using Imagemagick 7, snibgo's command can do the conversion of 50 to percent in-line as
Code: Select all
magick toes.png ( +clone -channel RGB -threshold "%[fx:100*50/255]"% +channel -grayscale Brightness ) -alpha off -compose CopyOpacity -composite out.png
So for completeness, by combining snibgo's command and my command in Unix syntax with IM 7 would be:
Code: Select all
echo "X,Y,R,G,B,A" > file.csv
magick image.png \( +clone -channel RGB -threshold "%[fx:100*50/255]"% +channel -grayscale Brightness \) -alpha off -compose CopyOpacity -composite sparse-color: | tr " " "\n" | sed 's/srgba//g' | sed 's/[()]//g' >> file.csv
Note since the image now contains transparency, I need to add ,A to the title and change the sed command from srgb to srgba. This adds one extra column of data to the CSV file that should have value all equal to 1, since only the opaque pixels are kept by sparse-color: