outputting pixel RGB values to CSV file

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
gimpeme
Posts: 8
Joined: 2017-08-11T11:09:45-07:00
Authentication code: 1151

outputting pixel RGB values to CSV file

Post 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:
  1. As far as my goal goes, is there any simpler way of accomplishing this?
  2. If I want to use the "sparse-color", can I get this output into a file?
  3. 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.
Last edited by gimpeme on 2018-08-29T18:08:25-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: outputting pixel RGB values to CSV file

Post 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

Image
gimpeme
Posts: 8
Joined: 2017-08-11T11:09:45-07:00
Authentication code: 1151

Re: outputting pixel RGB values to CSV file

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: outputting pixel RGB values to CSV file

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: outputting pixel RGB values to CSV file

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: outputting pixel RGB values to CSV file

Post by fmw42 »

Thanks for the filtering code, snibgo. I forgot to add code to do the filtering. My code only creates the csv file.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: outputting pixel RGB values to CSV file

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: outputting pixel RGB values to CSV file

Post 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:
Post Reply