Skip to content

Instantly share code, notes, and snippets.

@ultraist
Created October 28, 2009 04:27
Show Gist options
  • Select an option

  • Save ultraist/220239 to your computer and use it in GitHub Desktop.

Select an option

Save ultraist/220239 to your computer and use it in GitHub Desktop.
// モノクロ画像なら0を返すコマンド
#include <stdio.h>
#if 0
#include "opencv/cv.h"
#include "opencv/cxcore.h"
#include "opencv/highgui.h"
#else
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#endif
#define ISMONO_HUE_MAX 180
#define ISMONO_THRESHOLD 2
typedef enum {
ISMONO_MONO,
ISMONO_NONMONO,
ISMONO_LOAD_ERROR,
} ismono_status_t;
static ismono_status_t
ismono(const char *filename)
{
IplImage *bgr = cvLoadImage(filename, CV_LOAD_IMAGE_COLOR);
IplImage *hsv;
int hue_histogram[ISMONO_HUE_MAX] = {0};
int fqd;
int i, x, y;
if (!bgr) {
return ISMONO_LOAD_ERROR;
}
hsv = cvCreateImage(cvGetSize(bgr), bgr->depth, 3);
cvCvtColor(bgr, hsv, CV_BGR2HSV);
for (y = 0; y < hsv->height; ++y) {
for (x= 0; x < hsv->width; ++x) {
CvScalar px = cvGet2D(hsv, y, x);
if (0.0 <= px.val[0] && px.val[0] < (double)ISMONO_HUE_MAX) {
int hue = (int)px.val[0];
++hue_histogram[hue];
}
}
}
fqd = 0;
for (i = 0; i < ISMONO_HUE_MAX; ++i) {
if (hue_histogram[i] != 0) {
++fqd;
}
}
cvReleaseImage(&bgr);
cvReleaseImage(&hsv);
return fqd <= ISMONO_THRESHOLD ? ISMONO_MONO: ISMONO_NONMONO;
}
int
main(int argc, const char *argv[])
{
int shell_ret;
if (argc != 2) {
fprintf(stderr, "usage: %s file\n", argv[0]);
return -1;
}
switch (ismono(argv[1])) {
case ISMONO_MONO:
shell_ret = 0; // success
break;
case ISMONO_NONMONO:
shell_ret = 1;
break;
case ISMONO_LOAD_ERROR:
fprintf(stderr, "%s: %s: load error\n", argv[0], argv[1]);
shell_ret = -1;
break;
}
return shell_ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment