Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3436

Camera board • Plot raw files by OpenCV?

$
0
0
Dear all,

Wish you happy New Year 2025!

I have successfully capture 256 raw files with the HQ camera and store each in series such as 0000.raw ... 0255.raw.

The file size shown in the property is 24,709,120 bytes, so I suppose the image format is 4064*3040 (columns*rows). Provided that the HD camera specification is 4056*3040, so there must be 8 bytes for zero-padding in each row?

Since I would like to plot selected pixels of 100 raw files on my Pi4B with 8GB RAM, that is 25MB*256= 6.4GB which is within my RAM size. I came to OpenCV which provides a relatively simple Mat format to do so.However my code below fails with message:

Code:

error: (-215:Assertion failed) !fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows) in function 'create'
Which may related to the columns*rows vs. rows*columns issue?

Besides, the pointer operation in Mat() may be also a problem?

Code:

Mat cv_source(vpixels, hpixels, CV_32FC1, raw + frame_num * raw_size);
Herein I attach my code,

Code:

int main(int argc, char *argv[]){ std::string readpath, writepath, framepara; // paths for read and write directory and frame parmeterunsigned int start_frame; // starting framesize_t frames; // number of frames to processstd::cout << "Raw files plot" << std::endl;std::cout << "Please enter the read directory for input files (*.raw): " << std::endl;getline (std::cin, readpath);std::cout << "Please enter the write directory for output files (*.xml): " << std::endl;getline (std::cin, writepath);std::cout << "Enter the start frame and number of frames to process (seperate by space): " << std::endl;getline (std::cin, framepara);std::stringstream ss(framepara);ss >> start_frame >> frames;size_t frame_num = 1;// the frame to plot in phase mapsize_t hpixels = 4056;// number of raw horizontal pixelssize_t vpixels = 3040;// number of raw vertical pixelssize_t raw_size = vpixels * hpixels;                                                  // raw size        unsigned int i, j, k, l, row, col;char filename[512];char window1_name[512];  unsigned int end_frame = start_frame + frames;std::string input_v, input_h;// (v, h) input stringsize_t tdata_v, tdata_h;// (v, h) of selected pixelstruct point Center;    float_t *buffer = (float_t *)malloc(vpixels*sizeof(float_t));                           // 1D array holds every row vector    float_t *raw = (float_t *)malloc(frames * vpixels * hpixels * sizeof(float_t));// pseudo 3D array holds ROI pixels in all frames    float_t *tdata = (float_t *)malloc(frames * sizeof(float_t));// 1D array holds temporal data of selected pixelMat cv_source(vpixels, hpixels, CV_32FC1, raw + frame_num * raw_size);   // raw from selected frame numberMat cv_edge_x(vpixels, hpixels, CV_32FC1);Mat cv_edge_y(vpixels, hpixels, CV_32FC1);Mat cv_edge(vpixels, hpixels, CV_32FC1);Mat cv_display(vpixels, hpixels, CV_8UC1, Scalar(0));Mat cv_display_color(vpixels, hpixels, CV_8UC3, Scalar(0,0,0));Mat cv_display_mat(Size(vpixels, hpixels), CV_8UC3, Scalar(0,0,0));Mat cv_tdata(1, frames, CV_64F);Mat cv_display_tdata;Mat cv_display_hdata;i = 0;for (k = start_frame; k < end_frame; ++k){sprintf(filename, "%s%04d.raw", readpath.c_str(), k);FILE *ifp = fopen(filename,"rb");if (!ifp) {std::cout << "Files not found!" << std::endl;return -1;}for (row = 0; row < vpixels; row++)                                             // iterate over pixel rows{  fread(buffer, 2, vpixels, ifp);                                         // read .raw file to row vector of 2*3040 bytes one by onej = 0;// offset into bufferfor (col = 0; col < hpixels; col++)// iterate over every pixel columns{raw[i * raw_size + row * hpixels + col] = buffer[j++];// copy buffer into roi              }}i++;fclose(ifp);}phasemap = cv_source.clone();// 2D phasemap datadouble minVal, maxVal;// Max and Min of 2D phasemapcv::Point minPhi_p, maxPhi_p, Pos_d;// Position of Max and Min of 1D phaseseries, position change    Sobel(cv_source, cv_edge_x, CV_32FC1, 1, 0, -1, 1, 0, BORDER_DEFAULT);Sobel(cv_source, cv_edge_y, CV_32FC1, 0, 1, -1, 1, 0, BORDER_DEFAULT);cv_edge = cv_edge_x.mul(cv_edge_x) + cv_edge_y.mul(cv_edge_y);sqrt(cv_edge, cv_edge);edgemap = cv_edge.clone();minMaxLoc(cv_edge, &minVal, &maxVal);// find min and max of edgemapcv_edge.convertTo(cv_display, CV_8UC1, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));applyColorMap(cv_display, cv_display_color, COLORMAP_JET);cv_display_color.copyTo(cv_display_mat(Rect(0, 0, vpixels, hpixels)));// copy edgemap to the display matrix       namedWindow("ROI", WINDOW_NORMAL);resizeWindow("ROI", 800, 800);setMouseCallback( "ROI", onMouse, 0 );imshow("ROI", cv_display_mat);std::vector<int> compression_params;compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION);compression_params.push_back(3);    //png格式下,默认的参数为3.sprintf(filename, "%s%04zu_sf%04d_ef%04d.png", writepath.c_str(), frame_num, start_frame, end_frame);imwrite(filename, cv_display_mat, compression_params);    free(buffer);        free(raw);        free(tdata);          return 0;}

Statistics: Posted by rollyng — Sun Dec 29, 2024 11:35 am — Replies 1 — Views 32



Viewing all articles
Browse latest Browse all 3436

Trending Articles