Ask Your Question
0

Histograms practice

asked 2017-06-27 03:35:16 -0500

jjabaram gravatar image

updated 2017-06-27 03:49:48 -0500

I try to practice with this code but, but it doesn`t read my image.

The Folder :

image description

argc just return 1.

is it because i don`t put the images in the right directory or the name of images that wrong ? i put the all the images same directory as project.

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace std;
using namespace cv;

/**
 * @function main
 */
int main( int argc, char** argv )
{
Mat src_base, hsv_base;
Mat src_test1, hsv_test1;
Mat src_test2, hsv_test2;
Mat hsv_half_down;

/// Load three images with different environment settings
if( argc < 4 )
{
    printf("** Error. Usage: ./compareHist_Demo <image_settings0> <image_settings1> <image_settings2>\n");
    return -1;
}

src_base = imread( argv[1], IMREAD_COLOR );
src_test1 = imread( argv[2], IMREAD_COLOR );
src_test2 = imread( argv[3], IMREAD_COLOR );

if(src_base.empty() || src_test1.empty() || src_test2.empty())
{
  cout << "Can't read one of the images" << endl;
  return -1;
}

/// Convert to HSV
cvtColor( src_base, hsv_base, COLOR_BGR2HSV );
cvtColor( src_test1, hsv_test1, COLOR_BGR2HSV );
cvtColor( src_test2, hsv_test2, COLOR_BGR2HSV );

hsv_half_down = hsv_base( Range( hsv_base.rows/2, hsv_base.rows - 1 ), Range( 0, hsv_base.cols - 1 ) );

/// Using 50 bins for hue and 60 for saturation
int h_bins = 50; int s_bins = 60;
int histSize[] = { h_bins, s_bins };

// hue varies from 0 to 179, saturation from 0 to 255
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 256 };

const float* ranges[] = { h_ranges, s_ranges };

// Use the o-th and 1-st channels
int channels[] = { 0, 1 };


/// Histograms
MatND hist_base;
MatND hist_half_down;
MatND hist_test1;
MatND hist_test2;

/// Calculate the histograms for the HSV images
calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
normalize( hist_base, hist_base, 0, 1, NORM_MINMAX, -1, Mat() );

calcHist( &hsv_half_down, 1, channels, Mat(), hist_half_down, 2, histSize, ranges, true, false );
normalize( hist_half_down, hist_half_down, 0, 1, NORM_MINMAX, -1, Mat() );

calcHist( &hsv_test1, 1, channels, Mat(), hist_test1, 2, histSize, ranges, true, false );
normalize( hist_test1, hist_test1, 0, 1, NORM_MINMAX, -1, Mat() );

calcHist( &hsv_test2, 1, channels, Mat(), hist_test2, 2, histSize, ranges, true, false );
normalize( hist_test2, hist_test2, 0, 1, NORM_MINMAX, -1, Mat() );

/// Apply the histogram comparison methods
for( int i = 0; i < 4; i++ )
{
    int compare_method = i;
    double base_base = compareHist( hist_base, hist_base, compare_method );
    double base_half = compareHist( hist_base, hist_half_down, compare_method );
    double base_test1 = compareHist( hist_base, hist_test1, compare_method );
    double base_test2 = compareHist( hist_base, hist_test2, compare_method );

    printf( " Method [%d] Perfect, Base-Half, Base-Test(1), Base-Test(2) : %f, %f, %f, %f \n", i, base_base, base_half , base_test1, base_test2 );
}

printf( "Done \n" );

return 0;
}

Thank you.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-06-27 03:39:58 -0500

berak gravatar image

updated 2017-06-27 03:40:59 -0500

this program expects 3 images as cmdline arguments, you have to run it like:

 ./compareHist_Demo img1.png img2.png img3.png

in the directory, where your images are.

edit flag offensive delete link more

Comments

thank you for your reply. i have 3 image in my project directory but still return -1.

jjabaram gravatar imagejjabaram ( 2017-06-27 03:51:27 -0500 )edit
1

you either have to run it from a console (cmd.exe) in your project dir, or, if you want to run it from the IDE, you have to go to your project settings -> Debug settings -> set "Startup path" to your project dir, and add the three image names in "cmdline args" (no idea, if the names are correct, i'm not using VS)

berak gravatar imageberak ( 2017-06-27 04:08:22 -0500 )edit
1

Thanks it works.

jjabaram gravatar imagejjabaram ( 2017-06-28 00:19:55 -0500 )edit
0

answered 2017-06-28 02:25:25 -0500

if you use visual studio,try to do this (I paint a picture) image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-06-27 03:35:16 -0500

Seen: 134 times

Last updated: Jun 28 '17