point extraction in opencv - opencv

for an ellipse in binary image i applied this code
for(int j=0;j<poi.cols;j++)
{
for(int i=0;i<poi.rows;i++)
{
if(poi.at<uchar>(j,i)==255 && n==0)
{
lx=j;
ly=i;
n=1;
}
}
}
n=0;
for(int j=poi.cols;j>0;j--)
{
for(int i=0;i<poi.rows;i++)
{
if(poi.at<uchar>(j,i)==255 && n==0)
{
rx=j;
ry=i;
n=1;
}
}
}
n=lx;
int y;
cout << lx << "\t" << ly << "\t" << rx << "\t" << ry;
int d=(rx-lx)/5;
for(int i=0;i<5;i++)
{
n=n+d;
y=0;
for(int j=0;j<poi.rows;j++)
{
if(poi.at<uchar>(n,j)>=250 && y==0)
{
cout << "\n" << n << "\t" << j;
y=1;
}
}
}
for finding 10 intermediate points (5 above & 5 below) along the edges. However, I'm not getting correct answer. At times it is not even detecting white pixels
Here's an example binary elipse

Related

How to track the 2d points over the images for 3d-reconstruction as specified in opencv sfm pipeline?

I am trying to do 3D reconstruction using this code from opencv. As far as I understood, I need a textfile with the 2D points as per the given format. I am wondering if anyone could help me in getting these 2D points they mention in this program. I have a set of images with the calibration parameters but I have been not able to understand how could I track and save these 2D points e.g. the first point in frame 1 is the same point in frame 2 in the format they specified .
#include <opencv2/core.hpp>
#include <opencv2/sfm.hpp>
#include <opencv2/viz.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace cv;
using namespace cv::sfm;
static void help() {
cout
<< "\n------------------------------------------------------------\n"
<< " This program shows the camera trajectory reconstruction capabilities\n"
<< " in the OpenCV Structure From Motion (SFM) module.\n"
<< " \n"
<< " Usage:\n"
<< " example_sfm_trajectory_reconstruction <path_to_tracks_file> <f> <cx> <cy>\n"
<< " where: is the tracks file absolute path into your system. \n"
<< " \n"
<< " The file must have the following format: \n"
<< " row1 : x1 y1 x2 y2 ... x36 y36 for track 1\n"
<< " row2 : x1 y1 x2 y2 ... x36 y36 for track 2\n"
<< " etc\n"
<< " \n"
<< " i.e. a row gives the 2D measured position of a point as it is tracked\n"
<< " through frames 1 to 36. If there is no match found in a view then x\n"
<< " and y are -1.\n"
<< " \n"
<< " Each row corresponds to a different point.\n"
<< " \n"
<< " f is the focal lenght in pixels. \n"
<< " cx is the image principal point x coordinates in pixels. \n"
<< " cy is the image principal point y coordinates in pixels. \n"
<< "------------------------------------------------------------------\n\n"
<< endl;
}
/* Build the following structure data
*
* frame1 frame2 frameN
* track1 | (x11,y11) | -> | (x12,y12) | -> | (x1N,y1N) |
* track2 | (x21,y11) | -> | (x22,y22) | -> | (x2N,y2N) |
* trackN | (xN1,yN1) | -> | (xN2,yN2) | -> | (xNN,yNN) |
*
*
* In case a marker (x,y) does not appear in a frame its
* values will be (-1,-1).
*/
void
parser_2D_tracks(const string &_filename, std::vector<Mat> &points2d )
{
ifstream myfile(_filename.c_str());
if (!myfile.is_open())
{
cout << "Unable to read file: " << _filename << endl;
exit(0);
} else {
double x, y;
string line_str;
int n_frames = 0, n_tracks = 0;
// extract data from text file
vector<vector<Vec2d> > tracks;
for ( ; getline(myfile,line_str); ++n_tracks)
{
istringstream line(line_str);
vector<Vec2d> track;
for ( n_frames = 0; line >> x >> y; ++n_frames)
{
if ( x > 0 && y > 0)
track.push_back(Vec2d(x,y));
else
track.push_back(Vec2d(-1));
}
tracks.push_back(track);
}
// embed data in reconstruction api format
for (int i = 0; i < n_frames; ++i)
{
Mat_<double> frame(2, n_tracks);
for (int j = 0; j < n_tracks; ++j)
{
frame(0,j) = tracks[j][i][0];
frame(1,j) = tracks[j][i][1];
}
points2d.push_back(Mat(frame));
}
myfile.close();
}
}
/* Keyboard callback to control 3D visualization
*/
bool camera_pov = false;
void keyboard_callback(const viz::KeyboardEvent &event, void* cookie)
{
if ( event.action == 0 &&!event.symbol.compare("s") )
camera_pov = !camera_pov;
}
/* Sample main code
*/
int main(int argc, char** argv)
{
// Read input parameters
if ( argc != 5 )
{
help();
exit(0);
}
// Read 2D points from text file
std::vector<Mat> points2d;
parser_2D_tracks( argv[1], points2d );
// Set the camera calibration matrix
const double f = atof(argv[2]),
cx = atof(argv[3]), cy = atof(argv[4]);
Matx33d K = Matx33d( f, 0, cx,
0, f, cy,
0, 0, 1);
bool is_projective = true;
vector<Mat> Rs_est, ts_est, points3d_estimated;
reconstruct(points2d, Rs_est, ts_est, K, points3d_estimated, is_projective);
// Print output
cout << "\n----------------------------\n" << endl;
cout << "Reconstruction: " << endl;
cout << "============================" << endl;
cout << "Estimated 3D points: " << points3d_estimated.size() << endl;
cout << "Estimated cameras: " << Rs_est.size() << endl;
cout << "Refined intrinsics: " << endl << K << endl << endl;
cout << "3D Visualization: " << endl;
cout << "============================" << endl;
viz::Viz3d window_est("Estimation Coordinate Frame");
window_est.setBackgroundColor(); // black by default
window_est.registerKeyboardCallback(&keyboard_callback);
// Create the pointcloud
cout << "Recovering points ... ";
// recover estimated points3d
vector<Vec3f> point_cloud_est;
for (int i = 0; i < points3d_estimated.size(); ++i)
point_cloud_est.push_back(Vec3f(points3d_estimated[i]));
cout << "[DONE]" << endl;
cout << "Recovering cameras ... ";
vector<Affine3d> path_est;
for (size_t i = 0; i < Rs_est.size(); ++i)
path_est.push_back(Affine3d(Rs_est[i],ts_est[i]));
cout << "[DONE]" << endl;
cout << "Rendering Trajectory ... ";
cout << endl << "Press: " << endl;
cout << " 's' to switch the camera pov" << endl;
cout << " 'q' to close the windows " << endl;
if ( path_est.size() > 0 )
{
// animated trajectory
int idx = 0, forw = -1, n = static_cast<int>(path_est.size());
while(!window_est.wasStopped())
{
for (size_t i = 0; i < point_cloud_est.size(); ++i)
{
Vec3d point = point_cloud_est[i];
Affine3d point_pose(Mat::eye(3,3,CV_64F), point);
char buffer[50];
sprintf (buffer, "%d", static_cast<int>(i));
viz::WCube cube_widget(Point3f(0.1,0.1,0.0), Point3f(0.0,0.0,-0.1), true, viz::Color::blue());
cube_widget.setRenderingProperty(viz::LINE_WIDTH, 2.0);
window_est.showWidget("Cube"+string(buffer), cube_widget, point_pose);
}
Affine3d cam_pose = path_est[idx];
viz::WCameraPosition cpw(0.25); // Coordinate axes
viz::WCameraPosition cpw_frustum(K, 0.3, viz::Color::yellow()); // Camera frustum
if ( camera_pov )
window_est.setViewerPose(cam_pose);
else
{
// render complete trajectory
window_est.showWidget("cameras_frames_and_lines_est", viz::WTrajectory(path_est, viz::WTrajectory::PATH, 1.0, viz::Color::green()));
window_est.showWidget("CPW", cpw, cam_pose);
window_est.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
}
// update trajectory index (spring effect)
forw *= (idx==n || idx==0) ? -1: 1; idx += forw;
// frame rate 1s
window_est.spinOnce(1, true);
window_est.removeAllWidgets();
}
}
return 0;
}
I would be really grateful if someone can help me through this. Thank you.

DICOM to Point Cloud in VTK

Is there any method for converting DICOM (ct scan) images to Point Clouds using VTK?
VTK allows reading DICOM and DICOM series and volume rendering but is it possible to generate a Point Cloud from a series of DICOM images?
If it isn't possible in VTK, is there some other library that I can use for this purpose?
Here is a dicom to point cloud demonstration. Dicom files are pretty variable depending on how the imaging is collected, but this is what we have been using for CT scans for some time. This is the "manual version" ie where you will need to interact with the terminal to navigate the dicom directory. It is possible to automate this but is highly dependent on your application.
I have pcl 8.0 and vtkdicom installed. (i was able to do a limited implementation of this without vtkdicom, but its features make the application far more robust at handling diverse dicom directory structures).
You will need to point the function in the main towards the appropriate directory on your computer (should be the file containing the DICOMDIR file). Once you have loaded the dicom, the visualizer has keyboard inputs m and n to control intensity target to be visualized. (you can easily change the code to filter for any of the parameters: x,y,z,intensity) and can change the width or stepsize as needed.
#include <pcl/common/common_headers.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/passthrough.h>
#include <boost/thread/thread.hpp>
#include <vtkSmartPointer.h>
#include <vtkDICOMImageReader.h>
#include "vtkImageData.h"
#include "vtkDICOMDirectory.h"
#include "vtkDICOMItem.h"
#include "vtkStringArray.h"
#include "vtkIntArray.h"
#include "vtkDICOMReader.h"
bool loadDICOM(pcl::PointCloud<pcl::PointXYZI>::Ptr outCloud, std::string fullPathToDicomDir)
{
// load DICOM dir file
vtkSmartPointer<vtkDICOMDirectory> ddir =
vtkSmartPointer<vtkDICOMDirectory>::New();
ddir->SetDirectoryName(fullPathToDicomDir.c_str());
ddir->Update();
//select patient
int n = ddir->GetNumberOfPatients();
int patientSelection = 0;
if (n > 1)
{
std::cout << "Select Patient number, total count: " << n << std::endl;
std::string userInput;
std::getline(std::cin, userInput);
patientSelection = std::stoi(userInput);
}
const vtkDICOMItem& patientItem = ddir->GetPatientRecord(patientSelection);
std::cout << "Patient " << patientSelection << ": " << patientItem.Get(DC::PatientID).AsString() << "\n";
//select study
vtkIntArray* studies = ddir->GetStudiesForPatient(patientSelection);
vtkIdType m = studies->GetMaxId() + 1;
int studySelection = 0;
if (m > 1)
{
std::cout << "Select study, total count: " << m << std::endl;
std::string userInput;
std::getline(std::cin, userInput);
studySelection = std::stoi(userInput);
}
int j = studies->GetValue(studySelection);
const vtkDICOMItem& studyItem = ddir->GetStudyRecord(j);
const vtkDICOMItem& studyPItem = ddir->GetPatientRecordForStudy(j);
cout << " Study " << j << ": \""
<< studyItem.Get(DC::StudyDescription).AsString() << "\" \""
<< studyPItem.Get(DC::PatientName).AsString() << "\" "
<< studyItem.Get(DC::StudyDate).AsString() << "\n";
int k0 = ddir->GetFirstSeriesForStudy(j);
int k1 = ddir->GetLastSeriesForStudy(j);
int seriesSelection;
std::cout << "Select series, range: " << k0 << " to " << k1 << std::endl;
for (int i = k0; i <= k1; i++)
{
const vtkDICOMItem& seriesItem = ddir->GetSeriesRecord(i);
vtkStringArray* a = ddir->GetFileNamesForSeries(i);
cout << " Series " << i << ": \""
<< seriesItem.Get(DC::SeriesDescription).AsString() << "\" "
<< seriesItem.Get(DC::SeriesNumber).AsString() << " "
<< seriesItem.Get(DC::Modality).AsString() << ", Images: "
<< a->GetNumberOfTuples() << "\n";
}
std::string userInput;
std::getline(std::cin, userInput);
seriesSelection = std::stoi(userInput);
const vtkDICOMItem& seriesItem = ddir->GetSeriesRecord(seriesSelection);
cout << " Series " << seriesSelection << ": \""
<< seriesItem.Get(DC::SeriesDescription).AsString() << "\" "
<< seriesItem.Get(DC::SeriesNumber).AsString() << " "
<< seriesItem.Get(DC::Modality).AsString() << "\n";
vtkStringArray* a = ddir->GetFileNamesForSeries(seriesSelection);
vtkDICOMReader* reader = vtkDICOMReader::New();
reader->SetFileNames(a);
reader->Update();
vtkSmartPointer<vtkImageData> sliceData = reader->GetOutput();
int numberOfDims = sliceData->GetDataDimension();
int* dims = sliceData->GetDimensions();
std::cout << "Cloud dimensions: ";
int totalPoints = 1;
for (int i = 0; i < numberOfDims; i++)
{
std::cout << dims[i] << " , ";
totalPoints = totalPoints * dims[i];
}
std::cout << std::endl;
std::cout << "Number of dicom points: " << totalPoints << std::endl;
//read data into grayCloud
double* dataRange = sliceData->GetScalarRange();
double* spacingData = reader->GetDataSpacing();
std::cout << "Data intensity bounds... min: " << dataRange[0] << ", max: " << dataRange[1] << std::endl;
if (numberOfDims != 3)
{
std::cout << "Incorrect number of dimensions in dicom file, generation failed..." << std::endl;
return false;
}
else
{
Eigen::RowVector3f spacing = Eigen::RowVector3f(spacingData[0], spacingData[1], spacingData[2]);
Eigen::RowVector3i dimensions = Eigen::RowVector3i(dims[0], dims[1], dims[2]);
outCloud->points.clear();
std::cout << "x spacing: " << spacing(0) << std::endl;
std::cout << "y spacing: " << spacing(1) << std::endl;
std::cout << "z spacing: " << spacing(2) << std::endl;
for (int z = 0; z < dims[2]; z++)
{
if (z % 50 == 0)
{
double percentageComplete = (double)z / (double)dims[2];
std::cout << "Dicom Read Progress: " << (int)(100.0 * percentageComplete) << "%" << std::endl;
}
for (int y = 0; y < dims[1]; y++)
{
for (int x = 0; x < dims[0]; x++)
{
double tempIntensity = sliceData->GetScalarComponentAsDouble(x, y, z, 0);
int tempX = x;
pcl::PointXYZI tempPt = pcl::PointXYZI();
if (!isinf(tempIntensity) && !isnan(tempIntensity))
{
//map value into positive realm
//tempIntensity = ((tempIntensity - dataRange[0]) / (dataRange[1] - dataRange[0]));
if (tempIntensity > SHRT_MAX) { tempIntensity = SHRT_MAX; }
else if (tempIntensity < SHRT_MIN) { tempIntensity = SHRT_MIN; }
}
else
{
tempIntensity = 0;
}
tempPt.x = tempX;
tempPt.y = y;
tempPt.z = z;
tempPt.intensity = tempIntensity;
outCloud->points.push_back(tempPt);
}
}
}
}
std::cout << "Load Dicom Cloud Complete!" << std::endl;
return true;
}
int indexSlice = 0;
void keyboardEventOccurred(const pcl::visualization::KeyboardEvent& event, void* viewer)
{
if (event.getKeySym() == "n" && event.keyDown())
{
indexSlice -= 1;
}
else if (event.getKeySym() == "m" && event.keyDown())
{
indexSlice += 1;
}
}
void displayCloud(pcl::PointCloud<pcl::PointXYZI>::Ptr cloud, std::string field, int step, int width, std::string window_name = "default")
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer(window_name));
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "id");
viewer->registerKeyboardCallback(keyboardEventOccurred, (void*)viewer.get());
pcl::PointCloud<pcl::PointXYZI>::Ptr tempCloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PassThrough<pcl::PointXYZI> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName(field); //could gate this on intensity if u preferred
int lastIndex = indexSlice-1; //proc first cycle
while (!viewer->wasStopped()) {
if (indexSlice != lastIndex)
{
int low = step * indexSlice - width / 2;
int high = step * indexSlice + width / 2;
pass.setFilterLimits(low, high);
pass.filter(*tempCloud);
lastIndex = indexSlice;
std::cout << field<< " range: " <<low<<" , "<<high<< std::endl;
viewer->removeAllPointClouds();
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZI> point_cloud_color_handler(tempCloud, "intensity");
viewer->addPointCloud< pcl::PointXYZI >(tempCloud, point_cloud_color_handler, "id");
}
viewer->spinOnce(50);
}
viewer->close();
}
// --------------
// -----Main-----
// --------------
int main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
loadDICOM(cloud, "C:/Local Software/voyDICOM/resources/DICOM_Samples/2021APR14 MiniAchors_V0");
displayCloud(cloud,"intensity",100,50);
return 0;
}
Note that in most cases dicom files are relatively massive in terms of raw dimensions and so I very rarely (never?) have loaded a whole dicom file into a point cloud (until for this code). Generally what I do is handle it in a dense format (short array) and then create clouds based on selections from that data. This way you can do certain imaging operations that benefit from a locked data grid (opening, closing, etc) prior to going to the sparse data set (point cloud) where everything becomes profoundly more expensive.
Pretty picture of it working with one of my debug dicom sets:
I think I might have found a way, after all. Haven't tried it yet but in theory it should work.
Firstly, the DICOM image needs to be converted into .vtk format using VTK once the DICOM images have been converted into .vtk they can then be converted into .pcd (Point cloud format) using PCL (point cloud library).

Count length of black pixels on image

I am using OpenCV to manipulate some images.
Suppose that image.png is black/white image (only B or W for pixel colors).
For example, if we print the colors for 3rd line, it could be:
WWWWWBBBWWWWWWBBBBBBWWWWWBBWWWW
I'd like to save info on each sequence of black pixels, I mean, I'd like to be able to compute, for each row, the values:
number of black sequences in row i: (3 on example above)
x-coordinate of end pixels for each black sequence in row i: (6,8 and 15,20 and 26,27 on example above)
length of each black sequence on row i: (l1=3,l2=6,l3=2 on example above) (this is easy assuming item above is done)
I'm using some for loop and testing if color is black. When it is black, I save the x coordinate and start other loop inside to run from this coordinate to the end of line, testing if the color is white. When it finds white color, it stops and save the previous coordinate.
This works to compute only the length of first sequence of black pixels. I don't know how to go to next (I even don't know how many there are).
Here is the main part of code (with some trash code):
for(int y=0;y<img.rows;y++) //img.rows
{
for(int x=0;x<img.cols;x++)
{
Vec3b color = image.at<Vec3b>(Point(x,y));
printf("(%d,%d,%d)\n",color[1],color[2],color[3]);
if(color[0] == 0 && color[1] == 0 && color[2] == 0)
{
cor[0]='B';
ymax = y;
if (ymin == -1) { ymin = y; }
int xmin = x;
int diam_esq = img.cols/2-xmin;
double dist_esq = sqrt( (x-img.cols/2)*(x-img.cols/2) + (y-img.rows/2)*(y-img.rows/2) );
for(int z=x;z<img.cols;z++)
{
Vec3b colorz = image.at<Vec3b>(Point(z,y));
if(colorz[0] == 255 && colorz[1] == 255 && colorz[2] == 255)
{
int xmax = z-1;
int diam_dir = xmax-img.cols/2;
double dist_dir = sqrt( (z-1-img.cols/2)*(z-1-img.cols/2) + (y-img.rows/2)*(y-img.rows/2) );
int diam = xmax - xmin;
//printf("y=%*d, xmin=%*d, xmax=%*d, esq=%*d, dir=%*d, diam=%*d\n",5,y,5,xmin,5,xmax,5,diam_esq,5,diam_dir,5,diam);
printf("%*d%*d%*d%*d%*d%*d%*f%*f\n",5,y,5,xmin,5,xmax,5,diam_esq,5,diam_dir,5,diam,13,dist_esq,13,dist_dir);
break;
}
}
break;
}
}
//break; // only y=0
}
Here is some code that does what you want. It only prints out results. It doesn't save them anywhere but I assume you'll know how to do this.
To find black sequences in each row, there is no need to do nested for loops for each sequence, just keep track of whether or not you're inside a black sequence and if yes, of where it began. Also, I use Mat::ptr to allow for a more efficient traversal of your image, row by row.
for(int i = 0; i < image.rows; i++) {
Vec3b* ptr = image.ptr<Vec3b>(i);
bool blackSequence = false;
int blackSequenceStart = -1;
int numberOfBlackSequences = 0;
for(int j = 0; j < image.cols; j++) {
Vec3b color = ptr[j];
if(color[0] == 0 && !blackSequence) { // this is assuming that all pixels are either white or black
blackSequence = true;
blackSequenceStart = j;
numberOfBlackSequences++;
}
if(color[0] == 255 && blackSequence) {
blackSequence = false;
cout << "Row " << i << ": Sequence " << numberOfBlackSequences << " starts at " << blackSequenceStart
<< " and finishes at " << j - 1 << ". Total length: " << j - blackSequenceStart << endl;
}
if(j == image.cols - 1 && blackSequence) {
blackSequence = false;
cout << "Row " << i << ": Sequence " << numberOfBlackSequences << " starts at " << blackSequenceStart
<< " and finishes at " << j << ". Total length: " << j - blackSequenceStart + 1 << endl;
}
}
}

Fail assertion opencv mat.inl.hpp line 930

I have a trivial problem but I don't know how to solve it. I just wanna do a simple "foreach" of a Mat to view rgb values. I have next code:
for(int i=0; i<mat.rows; i++)
{
for(int j=0; j<mat.cols; j++)
{
int value_rgb = mat.at<uchar>(i,j);
cout << "(" << i << "," << j << ") : " << value_rgb <<endl;
}
}
The mat is 200 rows x 200 cols. When I print on console the results, just in the final the programs fails with next error:
**OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 <(unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 1 5) == elemSize1()) in unknown function, file c:\opencv\build\include\opencv2\core\mat.hpp, line 537**
Anyone can help me?
Thanks.
The below piece of code will help you in accessing the rgb pixel values.You have to access three channels to view RGB values.
for(int i = 0; i < i<mat.rows; i++)
{
for(int j = 0; j < mat.cols; j++)
{
int b = mat.at<cv::Vec3b>(i,j)[0];
int g = mat.at<cv::Vec3b>(i,j)[1];
int r = mat.at<cv::Vec3b>(i,j)[2];
cout << r << " " << g << " " << b << value_rgb <<endl ;
}
}
To read pixel value from a grayscale image
#include <opencv\cv.h>
#include <highgui\highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
cv::Mat img = cv::imread("5.jpg",0);
for(int j=0;j<img.rows;j++)
{
for (int i=0;i<img.cols;i++)
{
int a;
a=img.at<uchar>(j,i);
cout<<a<<endl;
}
}
cv::imshow("After",img);
waitKey(0);
}
Updated
This code reads all the grayscale values from an image and results in frequent occurring vales (Number of times the value as occurred). i.e
Number of times pixel value '0' as appeared,
Number of times pixel value '1' as appeared, ... & so on till 256.
#include <opencv\cv.h>
#include <highgui\highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
cv::Mat img = cv::imread("5.jpg",0);
//for(int j=0;j<img.rows;j++)
//{
// for (int i=0;i<img.cols;i++)
// {
// int a;
// a=img.at<uchar>(j,i);
// cout<<a<<endl;
// }
//}
vector<int> values_rgb;
for(int i=0; i<20; i++)
{
for(int j=0; j<20; j++)
{
int value_rgb = img.at<uchar>(i,j);
values_rgb.push_back(value_rgb);
//cout << "(" << i << "," << j << ") : " << value_rgb <<endl;
}
}
// Sorting of values in ascending order
vector<int> counter_rg_values;
for(int l=0; l<256; l++)
{
for(int k=0; k<values_rgb.size(); k++)
{
if(values_rgb.at(k) == l)
{
counter_rg_values.push_back(l);
}
}
}
//for(int m=0;m<counter_rg_values.size();m++)
//cout<<m<<" "<< counter_rg_values[m] <<endl;
int m=0;
for(int n=0;n<256;n++)
{
int c=0;
for(int q=0;q<counter_rg_values.size();q++)
{
if(n==counter_rg_values[q])
{
//int c;
c++;
m++;
}
}
cout<<n<<"= "<< c<<endl;
}
cout<<"Total number of elements "<< m<<endl;
cv::imshow("After",img);
waitKey(0);
}

OpenCV storing vectors of vectors of points vector<vector<Point> >

I am using OpenCV 2.4.2 FileStorage and am trying to store a vector of vectors of Points in a way that I can read back in. What is the best way to do this?
Below is what I have tried but reading it back in throws the error:
OpenCV Error: Unspecified error (The sequence element is not a numerical scalar) in cvReadRawDataSlice, file /Downloads/OpenCV-2.4.2/modules/core/src/persistence.cpp, line 3367
Saving:
bool writeVectorVectorPoint(string fname, vector<vector<Point> > vvP)
{
FileStorage fs(fname, FileStorage::WRITE);
fs << "nV" << static_cast<int>(vvP.size());
for(size_t i = 0; i < vvP.size(); i++)
{
ostringstream istr;
istr << "v" << i;
fs << istr.str() << "[";
for(size_t jj = 0; jj < vvP.at(i).size(); jj++)
{
ostringstream jjstr;
fs << vvP.at(i).at(jj);
}
fs << "]";
}
fs.release();
return(true);
}
Reading:
FileStorage fs(argv[2], FileStorage::READ);
if(!fs.isOpened())
{
cout << "Unable to open: " << argv[2] << endl;
return(-1);
}
int nV = static_cast<int>(fs["nV"]);
cout << "nV: " << nV << endl;
vector<Point> vPts;
for(int ii = 0; ii < nV; ii++)
{
ostringstream iistr;
iistr << "v" << ii;
cout << iistr.str() << ": ";
fs[iistr.str()] >> vPts;
cout << endl;
}
fs.release();
The xml file looks like:
<?xml version="1.0"?>
<opencv_storage>
<nV>4</nV>
<v0>
<_>
269 490</_>
<_>
400 522</_>
<_>
331 600</_>
<_>
294 610</_></v0>
<v1>
<_>
537 510</_>
<_>
590 458</_>
<_>
603 612</_>
<_>
508 626</_></v1>
<v2>
<_>
594 287</_>
<_>
444 240</_></v2>
<v3>
<_>
451 330</_>
<_>
632 342</_>
<_>
652 344</_>
<_>
470 381</_></v3>
</opencv_storage>
A bit late :
A similar more simple example with vector of vector :
Write function, add a node each time with a different name (just for info) :
void writeVectorOfVector(FileStorage &fs, string name, vector<vector<Point2f>> &vov)
{
fs << name;
fs << "{";
for (int i = 0; i < vov.size(); i++)
{
fs << name + "_" + to_string(i);
vector<Point2f> tmp = vov[i];
fs << tmp;
}
fs << "}";
}
Read function, use FileNodeIterator to read each vector< x > and push it back to vector of vector :
void readVectorOfVector(FileNode &fns, string name, vector<vector<Point2f>> &vov)
{
vov.clear();
FileNode fn = fns[name];
if (fn.empty()){
return;
}
FileNodeIterator current = fn.begin(), it_end = fn.end(); // Go through the node
for (; current != it_end; ++current)
{
vector<Point2f> tmp;
FileNode item = *current;
item >> tmp;
vov.push_back(tmp);
}
}
Tested with OpenCV 2.4.8/C++/Vs2013.
Hope this simplify your code.
For the moment, I solved this by just not using OpenCV's FileStorage. Here is what I did for storing a vector of vector of Point since there are good routines for printing the vectors.
bool writeVectorVectorPoint(string fname, vector<vector<Point> > vvP)
{
ofstream fsOut;
vector<vector<Point> >::iterator p;
fsOut.open(fname.c_str());
if(fsOut.fail())
{
cout << "Failed to open " << fname << endl;
return(false);
}
for(p = vvP.begin(); p != vvP.end(); p++)
{
fsOut << (*p) << endl;
}
fsOut.close();
return(true);
}
Reading them back in is a little more complicated, but still not difficult. If anyone sees any optimizations, please let me know since I suspect there is a better (or at least more elegant) solution.
bool readVectorVectorPoint(string fname, vector<vector<Point> >& vvP)
{
ifstream fsIn;
fsIn.open(fname.c_str());
if(fsIn.fail())
{
cout << "Failed to open: " << fname << endl;
return(false);
}
Point pt;
vector<Point> vPt;
vector<Point>::iterator p;
string line;
while(getline(fsIn, line))
{
cout << line << endl;
string ptStr;
stringstream s(line.substr(1,line.size()-1));
vPt.clear();
while(getline(s, ptStr, ';'))
{
stringstream s1(ptStr);
string lastStr;
getline(s1, lastStr, ',');
pt.x = atoi(lastStr.c_str());
getline(s1, lastStr, ',');
pt.y = atoi(lastStr.c_str());
vPt.push_back(pt);
}
vvP.push_back(vPt);
}
fsIn.close();
return(true);
}
in my case i used this solved to read a vector o KeyPoints and a Mat based on answer of bloublo
i m using opencv 2.4.9
FileNodeIterator current = fn.begin(), it_end = fn.end(); // Go through the node
for (; current != it_end; ++current)
{
vector<KeyPoint> kp;
Mat desc;
FileNode item = *current;
read(item,kp);
++current;
item = *current;
item >> desc;
...
}

Resources