{"id":430,"date":"2011-01-27T01:41:03","date_gmt":"2011-01-27T00:41:03","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=430"},"modified":"2011-02-14T13:59:04","modified_gmt":"2011-02-14T12:59:04","slug":"mittelwert-der-farben-in-einem-video","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=430","title":{"rendered":"OpenCV Mittelwert der Farben in einem Video"},"content":{"rendered":"<p>Schaut euch mal dieses geniale Video an<br \/>\n<a href=\"http:\/\/www.youtube.com\/watch?v=NtoYuwmTzt0\">http:\/\/www.youtube.com\/watch?v=NtoYuwmTzt0<\/a><\/p>\n<p>Wird das Video nicht zwischendrin irgendwie blasser? Es kommt mir gerade so vor. Hmmm man k\u00f6nnte ja mal die Farben analysieren. Also jedes einzelne Frame in den HSV (H Farbwert, S S\u00e4ttigung, V Wert) umrechnen, den Mittelwert \u00fcber alle Pixel bilden und dann grafisch auftragen.<\/p>\n<p>Gesagt, getan. <\/p>\n<p><a href=\"http:\/\/roboblog.fatal-fury.de\/wp-content\/uploads\/2011\/01\/mittelwertfarbe.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/roboblog.fatal-fury.de\/wp-content\/uploads\/2011\/01\/mittelwertfarbe-150x150.jpg\" alt=\"mittelwertfarbe\" title=\"mittelwertfarbe\" width=\"150\" height=\"150\" class=\"alignnone size-thumbnail wp-image-431\" \/><\/a><\/p>\n<p>Auf der X-Achse ist die Framenummer aufgetragen. Auf der Y-Achse HSV. Die rote Kurve ist H, gr\u00fcn S und blau V.<br \/>\nMan kann deutlich den Vor- und Abspann erkennen. Sonst bleiben die Werte relativ gleich. Die Kamera bewegt sich aber auch nicht.<br \/>\nAber drei mal geht die S\u00e4ttigung runter. Das Bild wird blass und bl\u00e4ulicher. Wie vermutet :)<\/p>\n<p>Sch\u00f6n sind auch noch die ganzen Spr\u00fcnge zu erkennen, wenn das Bild schnell ein und ausgeblendet wird.<\/p>\n<p>Und hier mein quick&dirty Programm. Habe mplayer genutzt um an die einzelnen Frames zu kommen, da openCV Version 1 so sein Probleme mit Linux und avi hat.<\/p>\n<p><code>mplayer ..\/Equilibrium\\ -\\ Unbesiegt\\ Piano\\ Cover.flv -vo jpeg -ao null<\/code><\/p>\n<p><code>#include < stdio.h ><br \/>\n#include < stdlib.h ><\/p>\n<p>#include < cv.h ><br \/>\n#include < highgui.h ><\/p>\n<p>#define getPixel(img,x,y)  (((uchar*)(img->imageData + img->widthStep*(y)))[x])<br \/>\n#define putPixel(img,x,y,color) (((uchar*)(img->imageData + img->widthStep*(y)))[x] = color)<\/p>\n<p>#define getPixelB(img,x,y)  (((uchar*)(img->imageData + img->widthStep*(y)))[x*3])<br \/>\n#define putPixelB(img,x,y,color) (((uchar*)(img->imageData + img->widthStep*(y)))[x*3] = color)<\/p>\n<p>#define getPixelG(img,x,y)  (((uchar*)(img->imageData + img->widthStep*(y)))[(x)*3+1])<br \/>\n#define putPixelG(img,x,y,color) (((uchar*)(img->imageData + img->widthStep*(y))) [(x)*3+1] = color)<\/p>\n<p>#define getPixelR(img,x,y)  (((uchar*)(img->imageData + img->widthStep*(y)))[(x)*3+2])<br \/>\n#define putPixelR(img,x,y,color) (((uchar*)(img->imageData + img->widthStep*(y)))[(x)*3+2] = color)<\/p>\n<p>using namespace std;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    char filename[18] ;<br \/>\n    IplImage *image = 0;<br \/>\n    IplImage* hsv = 0;<br \/>\n    cvNamedWindow(\"original\",1);<\/p>\n<p>    int i = 1;<br \/>\n    snprintf(filename, 18,\"jpg\/%08d.jpg\",i++);<br \/>\n    image = cvLoadImage(filename);<br \/>\n    hsv = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 3 );<\/p>\n<p>    IplImage* h_plane = cvCreateImage( cvGetSize( image ), 8, 1 );<br \/>\n    IplImage* s_plane = cvCreateImage( cvGetSize( image ), 8, 1 );<br \/>\n    IplImage* v_plane = cvCreateImage( cvGetSize( image ), 8, 1 );<\/p>\n<p>    cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );<\/p>\n<p>    while(1)<br \/>\n    {<br \/>\n        snprintf(filename, 18,\"jpg\/%08d.jpg\",i++);<\/p>\n<p>        image = cvLoadImage(filename);<br \/>\n        if(image == 0)<br \/>\n            break;<\/p>\n<p>        cvCvtColor( image, hsv, CV_BGR2HSV );<br \/>\n         cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );<\/p>\n<p>        float meanh = 0, means = 0, meanv = 0;<br \/>\n        for(int y = 0; y < hsv->height; y++)<br \/>\n        {<br \/>\n            for(int x = 0; x < hsv->width; x++)<br \/>\n            {<br \/>\n                meanh += getPixel(h_plane, x, y);<br \/>\n                means += getPixel(s_plane, x, y);<br \/>\n                meanv += getPixel(v_plane, x, y);<br \/>\n            }<br \/>\n        }<br \/>\n        meanh \/= image->width * image->height;<br \/>\n        means \/= image->width * image->height;<br \/>\n        meanv \/= image->width * image->height;<\/p>\n<p>        printf(\"%f %f %f\\n\", meanh, means, meanv);<\/p>\n<p>        cvShowImage(\"original\", image);<\/p>\n<p>        cvWaitKey(10);<\/p>\n<p>        cvReleaseImage(&image);<\/p>\n<p>    }<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Wer gern den QtCreator benutzt, hier noch mein .pro File.<\/p>\n<p><code>SOURCES += main.cpp<\/p>\n<p>INCLUDEPATH += \/usr\/include\/opencv<br \/>\nCONFIG += link_pkgconfig<br \/>\nPKGCONFIG += opencv<\/code><\/p>\n<p>Und hier mal ein \"normales\" Video<br \/>\n<a href=\"http:\/\/roboblog.fatal-fury.de\/wp-content\/uploads\/2011\/01\/mittelwertfarbe2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/roboblog.fatal-fury.de\/wp-content\/uploads\/2011\/01\/mittelwertfarbe2-150x150.jpg\" alt=\"mittelwertfarbe2\" title=\"mittelwertfarbe2\" width=\"150\" height=\"150\" class=\"alignnone size-thumbnail wp-image-437\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Schaut euch mal dieses geniale Video an http:\/\/www.youtube.com\/watch?v=NtoYuwmTzt0 Wird das Video nicht zwischendrin irgendwie blasser? Es kommt mir gerade so vor. Hmmm man k\u00f6nnte ja mal die Farben analysieren. Also jedes einzelne Frame in den HSV (H Farbwert, S S\u00e4ttigung, V Wert) umrechnen, den Mittelwert \u00fcber alle Pixel bilden und dann grafisch auftragen. Gesagt, getan. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14,16],"class_list":["post-430","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-c","tag-opencv"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=430"}],"version-history":[{"count":9,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":500,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/430\/revisions\/500"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}