I need to download an image and measure it's contrast. I have some Python code which does what I need, but now I need to convert it to C, so that I can use it from other languages which do not have OpenCV bindings. This is the Python version:
import cv2
import numpy as np
nparr = np.asarray(bytearray(image_file_in_memory), dtype=np.uint8)
img = cv2.imdecode(nparr, 0)
height, width, depth = img.shape
roi = img[0:bottom, 0:right]
edges = cv2.Sobel(roi, -1, 1, 1)
count = np.sum(edges);
This is what I have so far. But I can't get the pixel count to tally properly. It always returns the value height x width, instead of only the count of white pixels, like the Python version does.
int count = 0;
IplImage *img, *tmp;
CvMat *mat = cvCreateMat(1, src_len, CV_32SC3);
/* src is the in-memory image */
mat->data.ptr = (unsigned char *)src;
img = cvDecodeImage(mat, -1);
if (! img) return -1;
cvReleaseMat(&mat);
cvSetImageROI(img, cvRect(0, 0, right, bottom));
tmp = cvCloneImage(img);
cvSobel(img, tmp, 1, 1, 3);
count = cvCountNonZero(tmp);
/* or */
count = cvSum(tmp).val[0];