USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

ImageMagick analysis

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Graphics Applications
View previous topic :: View next topic  
Author Message
samwichse
Member


Joined: 29 Aug 2024
Posts: 107

PostPosted: Fri Jun 16, 2024 1:59 am    Post subject: ImageMagick analysis Reply with quote

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
View user's profile Send private message
samwichse
Member


Joined: 29 Aug 2024
Posts: 107

PostPosted: Sat Jul 01, 2024 3:56 am    Post subject: Reply with quote

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
View user's profile Send private message
Germ
Keeper of the BIG STICK


Joined: 30 Apr 2024
Posts: 12452
Location: Planet Earth

PostPosted: Sat Jul 01, 2024 10:07 am    Post subject: Reply with quote

Glad you found a solution. I didn't have a clue...



_________________
Laptop: Mandriva 2024 PowerPack - 2.6.33.5-0.2mnb
Desktop: Mandriva 2024 Free - kernel 2.6.33.2-1mib
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
VHockey86
Advanced Member


Joined: 12 Dec 2024
Posts: 988
Location: Rochester

PostPosted: Sat Jul 01, 2024 11:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 11833
Location: Iowa

PostPosted: Sun Jul 02, 2024 1:53 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address
VHockey86
Advanced Member


Joined: 12 Dec 2024
Posts: 988
Location: Rochester

PostPosted: Sun Jul 02, 2024 5:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Graphics Applications All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse