QImage ribi::HistogramEqualizationerMainDialog::DoHistogramEqualization(const QImage& source) noexcept { QImage image(source); const int width = image.width(); const int height = image.height(); const int surface = width * height; const int nGreyValues = 256; //There are 256 different pixel intensities const std::vector<int> histogram = GetImageHistogram(image); assert(nGreyValues==static_cast<int>(histogram.size())); const std::vector<int> cumulativeHistogram = GetCumulativeHistogram(histogram); assert(nGreyValues==static_cast<int>(cumulativeHistogram.size())); //Works, but anybody knows how to use std::for_each or std::transform for this? std::vector<int> rescaledHistogram(nGreyValues,0); for (int i=0; i!=nGreyValues; ++i) { //'surface + 1' to prevent that rescaledGreyValue == 256 const int rescaledGreyValue = static_cast<int>( static_cast<double>(nGreyValues) * static_cast<double>(cumulativeHistogram[i]) / static_cast<double>(surface + 1) ); assert(rescaledGreyValue >= 0); assert(rescaledGreyValue < 256); rescaledHistogram[i] = rescaledGreyValue; } for (int y=0; y!=height; ++y) { for (int x=0; x!=width; ++x) { const QRgb rgb { image.pixel(x,y) }; const int grey { ( qRed(rgb) + qGreen(rgb) + qBlue(rgb) ) / 3 }; assert(grey >= 0); assert(grey < 256); const int greyNew = rescaledHistogram[grey]; assert(greyNew >= 0); assert(greyNew < 256); image.setPixel(x,y,qRgb(greyNew,greyNew,greyNew)); } } return image; }
gboolean isTennisFree() { gboolean res = FALSE; ExceptionInfo *exception; Image *image; ImageInfo *image_info; unsigned long colors; ColorPacket *histogram; unsigned long i = 0; char* img_path = "video.jpg"; getImg(); exception = AcquireExceptionInfo(); image_info = CloneImageInfo((ImageInfo *) NULL); strcpy(image_info->filename, img_path); image = ReadImage(image_info,exception); if (exception->severity != UndefinedException) { CatchException(exception); } if (image == (Image *) NULL) { return FALSE; //exit(1); } histogram = GetImageHistogram(image, &colors, exception); qsort((void *) histogram, (size_t) colors, sizeof(*histogram), myHistogramCompare); if (histogram == (ColorPacket*) NULL) { MagickError(exception->severity, exception->reason, exception->description); } if(colors > 0) { if(histogram[i].count > 5000 && ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.red)) < 35 && ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.green)) < 35 && ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.blue)) < 35) { res = TRUE; printf("Empty\n"); } else { res = FALSE; printf("Busy\n"); } // printf("%d %d %d %d\n", (int)histogram[i].count, ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.red)), ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.green)), ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.blue))); } // remove(img_path); histogram = (ColorPacket *) RelinquishMagickMemory(histogram); DestroyImage(image); image_info=DestroyImageInfo(image_info); exception=DestroyExceptionInfo(exception); return res; }
int main(int argc,char **argv) { ExceptionInfo *exception; Image *image; ImageInfo *image_info; unsigned long colors; ColorPacket *histogram; unsigned long i = 0; char* img_path = "video.jpg"; getImg(); MagickCoreGenesis(*argv,MagickTrue); exception = AcquireExceptionInfo(); image_info = CloneImageInfo((ImageInfo *) NULL); strcpy(image_info->filename, img_path); image = ReadImage(image_info,exception); if (exception->severity != UndefinedException) { CatchException(exception); } if (image == (Image *) NULL) { exit(1); } histogram = GetImageHistogram(image, &colors, exception); qsort((void *) histogram, (size_t) colors, sizeof(*histogram), myHistogramCompare); if (histogram == (ColorPacket*) NULL) { MagickError(exception->severity, exception->reason, exception->description); } if(colors > 0) { if(histogram[i].count > 10000 && ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.red)) < 35 && ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.green)) < 35 && ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.blue)) < 35) { printf("Empty\n"); } else { printf("Busy\n"); } // printf("%u %d %d %d\n", histogram[i].count, ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.red)), ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.green)), ScaleQuantumToChar(RoundToQuantum(histogram[i].pixel.blue))); } remove(img_path); histogram = (ColorPacket *) RelinquishMagickMemory(histogram); DestroyImage(image); image_info=DestroyImageInfo(image_info); exception=DestroyExceptionInfo(exception); MagickCoreTerminus(); return(0); }