View previous topic :: View next topic |
Author |
Message |
samwichse Member

Joined: 29 Aug 2025 Posts: 107
|
Posted: Fri Jun 16, 2025 1:59 am Post subject: ImageMagick analysis |
|
|
Hey all, any imagemagick gurus out there?
I'm trying to use imagemagick to figure out the percent cover of vegetation from some photographs.
What I'd like to do is set any pixel with a green tinge to be black (basically RGB values where G>R and B) and other values to white, then count the number of both and get a percentage.
Unfortunately, it seems you need to know some C (I don't) and create a custom filter. My posting on the ImageMagick forum got this response:
http://redux.imagemagick.org/discussion-server/viewtopic.html?t=6775
The crux is, I need to change that code, then get it somehow compiled (can you compile just that file... seems like you have to recompile/reinstall the whole package?) to make it do what I want, but I don't even know where to begin.
Seems like it should be easy, but looking at the code to just get the average values gives me a headache.
Sam
Note: I'm trying to do this with free software in a scriptable way... if it works, I'll have hundreds of photos to do.
|
|
Back to top |
|
samwichse Member

Joined: 29 Aug 2025 Posts: 107
|
Posted: Sat Jul 01, 2025 3:56 am Post subject: |
|
|
Well, the friend of a friend gave me a hand (thanks Bill)
C code to do this:
Code: |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//ImageMagick Header used in version 6.2.5
#include "magick/ImageMagick.h"
int main( int argc, char **argv)
{
//ExceptionInfo *exception;
ExceptionInfo exception;
Image *image;
ImageInfo *image_info;
const PixelPacket *p;
long x, y;
long greenPix;
long total_pixels;
double percentGreen;
if (argc != 2)
{
fprintf(stdout,"Usage: %s image\n",argv[0]);
exit(0);
}
/*
Initialize the image info structure and read an image.
*/
InitializeMagick( argv[0] );
// Fix
//exception = AcquireExceptionInfo();
GetExceptionInfo( &exception );
image_info = CloneImageInfo( (ImageInfo *) NULL );
strcpy(image_info->filename, argv[1]);
image = ReadImage( image_info, &exception );
if (exception.severity != UndefinedException)
{
CatchException( &exception );
}
if (image == (Image *) NULL)
{
fprintf(stderr,"Failed to Load Image\n");
exit(1);
}
// Do real work...
greenPix = 0;
total_pixels = 0;
// For each row
for ( y = 0; y < (long)image->rows; y++ )
{
p = AcquireImagePixels( image, 0, y, image->columns, 1, &exception );
if (p == (const PixelPacket *) NULL)
{
break;
}
// For each pixel in the row
for( x = 0; x < (long)image->columns; x++ )
{
// check that green is bigger than both red and blue
if ( ( p->green > p->red ) && ( p->green > p->blue ) )
{
greenPix ++;
}
total_pixels++;
p++;
}
}
percentGreen = (double)greenPix / (double)total_pixels * 100;
printf("%s Green Content = %f%%\n", image_info->filename, percentGreen);
// Cleanup...
DestroyImage( image );
image_info = DestroyImageInfo( image_info );
//Fix -- ignore void return type
DestroyExceptionInfo( &exception );
//exception = DestroyExceptionInfo( exception );
DestroyMagick();
return(0);
}
|
|
|
Back to top |
|
Germ Keeper of the BIG STICK

Joined: 30 Apr 2025 Posts: 12452 Location: Planet Earth
|
Posted: Sat Jul 01, 2025 10:07 am Post subject: |
|
|
Glad you found a solution. I didn't have a clue...
_________________ Laptop: Mandriva 2025 PowerPack - 2.6.33.5-0.2mnb
Desktop: Mandriva 2025 Free - kernel 2.6.33.2-1mib
|
|
Back to top |
|
VHockey86 Advanced Member

Joined: 12 Dec 2025 Posts: 988 Location: Rochester
|
Posted: Sat Jul 01, 2025 11:53 pm Post subject: |
|
|
Lots of languages have interfaces to the imageMagick API...so you don't neccesarily have to use C
_________________ Main Desktops : Kubuntu 10.4. ArchLinux 64-bit. Windows7 64-bit. Windows XP 32-bit.
MacBook: OS X Snow Leopard (10.6)
|
|
Back to top |
|
crouse Site Admin

Joined: 17 Apr 2025 Posts: 11833 Location: Iowa
|
Posted: Sun Jul 02, 2025 1:53 pm Post subject: |
|
|
I don't know if this could have been done in bash or not..... i've used image magick alot using bash, but i think that the c interfaces are much better documented, however...... having not enough experience with c, i knew i wasn't going to be much help.....
Glad you have a solution, and thank you very much for sharing it ! That's always appreciated.
_________________ Veronica - Arch Linux 64-bit -- Kernel 2.6.33.4-1
Archie/Jughead - Arch Linux 32-bit -- Kernel 2.6.33.4-1
Betty/Reggie - Arch Linux (VBox) 32-bit -- Kernel 2.6.33.4-1
BumbleBee - OpenSolaris-SunOS 5.11
|
|
Back to top |
|
VHockey86 Advanced Member

Joined: 12 Dec 2025 Posts: 988 Location: Rochester
|
Posted: Sun Jul 02, 2025 5:56 pm Post subject: |
|
|
I believe that there are a number of functions that can be achieved through the API, that are not directly accessible with the command line utilities. Thus BASH wouldn't work. For what hes trying to accomplish, it may very well be possible though.
_________________ Main Desktops : Kubuntu 10.4. ArchLinux 64-bit. Windows7 64-bit. Windows XP 32-bit.
MacBook: OS X Snow Leopard (10.6)
|
|
Back to top |
|
|