Ask Your Question
0

read video & recostruction

asked 2015-12-30 05:27:13 -0600

baro gravatar image

updated 2015-12-30 05:34:17 -0600

LBerger gravatar image

Hi at all!!! I'm reading video from my memory and I execute some operation on it. When I read video I take a single frame (320x240) , I separe it with 10x10 grid and I save the single "box" (10x10) in a vector (Mat recostruction[768]). I create an other vector wich contains bool value. Each cell of vector contains bool value. Based on the value of the cells I recostruct the single box with different background color (true= red background image, false=normal backgrund image). When I show my output the output window is black and I don't show my rebuilt video. How I fix this problem? thank you for your time!

CvCapture* capture = cvCreateFileCapture("Scene1.avi");
IplImage* frame;
while(true)
{
    frame = cvQueryFrame(capture);
    if(!frame) break;
    Mat current(frame);
    Mat ricostruzione[768];
    Mat output = cv::Mat::zeros(240, 320, CV_8UC3);
    int aa[768];
    int bb[768];
    for(int a=0; a<320; a+=10)
    {
        for(int b=0; b<240; b+=10)
        {
            Mat SUPPORT_2 (current, Rect(a,b,10,10));
            ricostruzione[conteggio_c] = SUPPORT_2.clone();
            aa[conteggio_c] = a;
            bb[conteggio_c] = b;
            /*---------------------------------------------
            some operations
            ----------------------------------------------*/
            conteggio_c++;
        }
    }
    conteggio_c = 0;
    bool recost[768];
    for ( int t=0; t<768; t++)
    {
        /*--------------------------------------
        some operations and filling bool vector
        ---------------------------------------*/
    }
    imshow("original", current);
    waitKey(1);
    imshow("OUTPUT", output);
    waitKey(1);
}
edit retag flag offensive close merge delete

Comments

2
  1. Avoid the old C-api functions ans structures (cvWhatever and IplImage) and replace them with propoer C++ ones (VideoCapture, Mat)
  2. Your code is incomplete, there is no modification in the output image, so pretty hard to know what's going on
  3. Bonus: if you post your code in English, it's much easier for everyone to understand it and therefore to help. I can deal with Italian, but others won't...
LorenaGdL gravatar imageLorenaGdL ( 2015-12-30 05:53:05 -0600 )edit

just saying, if you want to chop your image into 10x10 tiles, you have to spare a 10pix border to the right / bottom, else you go out of bounds: for(int a=0; a<320 - 10; a+=10) (same for b)

berak gravatar imageberak ( 2015-12-30 23:37:21 -0600 )edit

@LorenaGdL excuse me if there isn't the modification of output but I think that are not necessary to fix the problem. The modifications are comparing and show a rect grid with red background based on the comparison . @berak Why -10? My image is 320x240. I must read image until last pix. If I use two images and 10x10 grid the program run correctly but if I use one image and a video from which use frame by frame (comparison between reference image and the current frame of video) don't run. My output is black.

baro gravatar imagebaro ( 2016-01-03 08:06:11 -0600 )edit

@baro start by modifying your code to only use C++. And call me crazy, but if you show some lines where output is initialized to a black image, you hide the possible modifications to the matrix, and then you asked why output is black... well, I do find the missing parts neccesary. And you're lucky to not run into out of bounds problems because your image dimensions are perfectly divisible by 10. @berak's comment should be taken into account in any code with a minimum level of reusability

LorenaGdL gravatar imageLorenaGdL ( 2016-01-03 11:30:26 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2016-01-04 11:34:31 -0600

baro gravatar image

@LorenaGdL

include <iostream>

include <stdio.h>

include <math.h>

include <opencv2\opencv.hpp>

include "opencv\highgui.h"

include "opencv\cv.h"

using namespace cv; using namespace std;

void add_piece_of_frame(const Mat&, Mat&, int, int); void add_piece_of_frame_red(const Mat&, Mat&, int, int);

int main(int argc, char** argv) { //lettura immagine Mat background = imread("C:\Users\Fabrizio\Desktop\frame\VIDEO_PER_TESI\Scene1.jpg",CV_LOAD_IMAGE_COLOR); //controllo di avvenuta lettura corretta if(! background.data )
{ cout << "Could not open or find the image" << std::endl ; return -1; } //mostra immagine riferimento imshow("background di riferimento", background); waitKey(1); //vettori per istogrami sfondo Mat hB_b[768]; Mat hB_g[768]; Mat hB_r[768]; //vettori per istogrami immagine Mat hC_b[768];
Mat hC_g[768];
Mat hC_r[768];

/**************variabili necessarie per l'istogrmma da generare*********************************************************************************/
//numero di bins
int histSize = 256;
//range per (b,g,r)
float range[] = { 0, 256 } ;
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
//**********************************************************************************************************************************************
Mat b_hist_b, g_hist_b, r_hist_b;
Mat b_hist_c, g_hist_c, r_hist_c;
Mat back_hist;
Mat current_hist;

double somma_bkg_b = 0;
double somma_bkg_g = 0;
double somma_bkg_r = 0;

double sommatoria_bkg_b[768];
double sommatoria_bkg_g[768];
double sommatoria_bkg_r[768];

int conteggio = 0;
int conteggio_c = 0;
int cont_hist = 0;

/***********************************************************************************************************************************************/
//*******************inizio raccolta dati di riferimento*****************************************************************************************

for(int a=0; a<320; a+=10)
{
    for(int b=0; b<240; b+=10)
    {

        Mat SUPPORT (background, Rect(a,b,10,10));

        vector<Mat> bgr_planes;
        split( SUPPORT, bgr_planes ); 

        calcHist( &bgr_planes[0], 1, 0, Mat(), hB_b[conteggio], 1, &histSize, &histRange, uniform, accumulate );
        calcHist( &bgr_planes[1], 1, 0, Mat(), hB_g[conteggio], 1, &histSize, &histRange, uniform, accumulate );
        calcHist( &bgr_planes[2], 1, 0, Mat(), hB_r[conteggio], 1, &histSize, &histRange, uniform, accumulate );


        //hB_b[conteggio] = b_hist_b.clone();
        //hB_g[conteggio] = g_hist_b.clone();
        //hB_r[conteggio] = r_hist_b.clone();

        for(int b=0; b<256; b++)
        {
            somma_bkg_b = somma_bkg_b + hB_b[conteggio].at<int>(b);
            somma_bkg_g = somma_bkg_g + hB_g[conteggio].at<int>(b);
            somma_bkg_r = somma_bkg_b + hB_r[conteggio].at<int>(b);
        }
        sommatoria_bkg_b[conteggio] = somma_bkg_b; 
        sommatoria_bkg_g[conteggio] = somma_bkg_g;
        sommatoria_bkg_r[conteggio] = somma_bkg_r;

        conteggio++;
    }
}

//----------------------fine della prima parte (salvataggio dati)--------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------

CvCapture* capture = cvCreateFileCapture("C:\\Users\\Fabrizio\\Desktop\\frame\\VIDEO_PER_TESI\\Scene1.avi");
IplImage* frame;

cout <<"***************************************************************************************** "<< std::endl ;
cout <<"************************inizio parte corrente******************************************** "<< std::endl ;
cout <<"***************************************************************************************** "<< std::endl ;

int A=0;

while(true)
{
    frame = cvQueryFrame(capture); 
    if(!frame) break;      //check for valid frame
    Mat current(frame);
    //---------vettore che serve per ricostruire l'immagine (salvataggio di parti di immagine originali)--------------------------------------
    Mat ricostruzione[768];
    //matrice che serve per generare output modificato
    Mat output = cv::Mat::zeros(240, 320, CV_8UC3);

    //vettori per il salvataggio dei punti iniziali di ricostruzione
    int aa[768];
    int bb[768];

    for(int a=0; a<320; a+=10)
    {
        for(int b=0; b<240; b+=10)
        {
            Mat SUPPORT_2 (current, Rect(a,b,10,10));

            ricostruzione[conteggio_c] = SUPPORT_2.clone();

            aa[conteggio_c] = a;
            bb[conteggio_c] = b;

            vector<Mat> bgr_planes_c;
            split( SUPPORT_2, bgr_planes_c );

            calcHist( &bgr_planes_c[0], 1, 0, Mat(), hC_b[conteggio_c], 1, &histSize, &histRange, uniform, accumulate );
            calcHist( &bgr_planes_c[1], 1, 0, Mat(), hC_g[conteggio_c], 1, &histSize, &histRange, uniform, accumulate );
            calcHist( &bgr_planes_c[2], 1, 0, Mat(), hC_r[conteggio_c], 1, &histSize, &histRange, uniform, accumulate );

            conteggio_c++; 
        }
    }

    float f_chi_squared_b[768]; 
    float f_chi_squared_g[768];
    float f_chi_squared_r[768];

    bool f_chi_sq[768];

    Mat ...
(more)
edit flag offensive delete link more

Comments

  1. Don't post updates as answers. Instead edit your previous question
  2. You haven't updated the code to C++...
  3. If your problem is with the video, you'd better try to create some minimum basic code to see if you can reproduce the issue
LorenaGdL gravatar imageLorenaGdL ( 2016-01-05 02:33:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-30 05:27:13 -0600

Seen: 410 times

Last updated: Jan 04 '16