OpenCv see each pixel value in an image - c++

I'm working on Connected Component Labeling (CCL) operation in OpenCv (in C++ language). To see whether CCL works reliably, I must check each pixel value in the image while debugging. I have tried saving the result of CCL as an image, however I could not reach digital values of the pixels. Is there any way of doing this during debugging operation?

As already mentioned by #Gombat and e.g. here, in Visual Studio you can install Image Watch.
If you want to save the values of a Mat to a text file, you don't need to reinvent anything (check OpenCV Mat: the basic image container).
You can for example save a csv file simply like:
Mat img;
// ... fill matrix somehow
ofstream fs("test.csv");
fs << format(img, "csv");
Full example:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
int main()
{
// Just a green image
Mat3b img(10,5,Vec3b(0,255,0));
ofstream fs("test.csv");
fs << format(img, "csv");
return 0;
}

Convert the CCL matrix into values in the range [0, 255] and save it as an image. For example:
cv::Mat ccl = ...; // ccl operation returning CV_8U
double min, max;
cv::minMaxLoc(ccl, &min, &max);
cv::Mat image = ccl * (255. / max);
cv::imwrite("ccl.png", image);
Or store all the values in a file:
std::ofstream f("ccl.txt");
f << "row col value" << std::endl;
for (int r = 0; r < ccl.rows; ++r) {
unsigned char* row = ccl.ptr<unsigned char>(r);
for (int c = 0; c < ccl.cols; ++c) {
f << r << " " << c << " " << static_cast<int>(row[c]) << std::endl;
}
}

Of course there is, but it depends on the type of image you use.
http://docs.opencv.org/doc/user_guide/ug_mat.html#accessing-pixel-intensity-values
Which IDE do you use for debugging? There is a Visual Studio opencv plugin:
http://opencv.org/image-debugger-plug-in-for-visual-studio.html
https://visualstudiogallery.msdn.microsoft.com/e682d542-7ef3-402c-b857-bbfba714f78d
To simply print a cv::Mat of type CV_8UC1 to a text file, use the code below:
// create the image
int rows(4), cols(3);
cv::Mat img(rows, cols, CV_8UC1);
// fill image
for ( int r = 0; r < rows; r++ )
{
for ( int c = 0; c < cols; c++ )
{
img.at<unsigned char>(r, c) = std::min(rows + cols - (r + c), 255);
}
}
// write image to file
std::ofstream out( "output.txt" );
for ( int r = -1; r < rows; r++ )
{
if ( r == -1 ){ out << '\t'; }
else if ( r >= 0 ){ out << r << '\t'; }
for ( int c = -1; c < cols; c++ )
{
if ( r == -1 && c >= 0 ){ out << c << '\t'; }
else if ( r >= 0 && c >= 0 )
{
out << static_cast<int>(img.at<unsigned char>(r, c)) << '\t';
}
}
out << std::endl;
}
Simply replace img, rows, cols with your vars and leave the "fill image" part aside and it should work. In the first row and column are the indices of that row / column. "output.txt" will be left in your debugging working directory you can specify in the projects debugging settings in visual studio.

Related

How to apply custom filters on image?

I'm using OpenCV4 on Ubuntu 20.04 LTS on WSL + XServer for GUI.
I want to create custom convlutional filter kernels and apply them to my image. this is the code I've written for it:
cv::Mat filter2D(cv::Mat input, cv::Mat filter)
{
using namespace cv;
Mat dst = input.clone();
//cout << " filter data successfully found. Rows:" << filter.rows << " cols:" << filter.cols << " channels:" << filter.channels() << "\n";
//cout << " input data successfully found. Rows:" << input.rows << " cols:" << input.cols << " channels:" << input.channels() << "\n";
for (int i = 0-(filter.rows/2);i<input.rows-(filter.rows/2);i++)
{
for (int j = 0-(filter.cols/2);j<input.cols-(filter.cols/2);j++)
{ //adding k and l to i and j will make up the difference and allow us to process the whole image
float filtertotal = 0;
for (int k = 0; k < filter.rows;k++)
{
for (int l = 0; l < filter.rows;l++)
{
if(i+k >= 0 && i+k < input.rows && j+l >= 0 && j+l < input.cols)
{ //don't try to process pixels off the edge of the map
float a = input.at<uchar>(i+k,j+l);
float b = filter.at<float>(k,l);
float product = a * b;
filtertotal += product;
}
}
}
//filter all proccessed for this pixel, write it to dst
dst.at<uchar>(i+(filter.rows/2),j+(filter.cols/2)) = filtertotal;
}
}
return dst;
}
int main(int argc, char** argv)
{
// Declare variables
cv::Mat_<float> src;
const char* window_name = "filter2D Demo";
// Loads an image
src = cv::imread("fapan.png", cv::IMREAD_GRAYSCALE ); // Load an image
if( src.empty() )
{
printf(" Error opening image\n");
return EXIT_FAILURE;
}
static float x[3][3] = {
{-1, -1, -1},
{-1, 8, -1},
{-1, -1, -1}
};
cv::Mat kernel(3,3, CV_16FC1, x);
// Apply filter
filter2D(src, kernel);
cv::imshow( window_name, src );
cv::waitKey(0);
return EXIT_SUCCESS;
}
the problem is that the output image is like this.
as you can see not only the edges are white, but also inside of it is white too.
the input image
The output you have posted for the input code is correct as you are applying a normal filter on a image .
It may cause a little blurring or sharpening in it but it will never cause it to completely detect edges.
In order to detect only the edges along the images you must apply Laplacian along a certain direction.
https://www.l3harrisgeospatial.com/docs/LaplacianFilters.html#:~:text=A%20Laplacian%20filter%20is%20an,an%20edge%20or%20continuous%20progression. ( A link with some info )
Which is the derivative of the image it will only detect the change .
I recommend you do this on matlab image processing toolbox .

Accessing RGB values of all pixels in a certain image in openCV

I have searched internet and stackoverflow thoroughly, but I didn't find what exactly I'm looking for!
How can I get RGB (BGR actually) values of a certain image (all pixels of the image) in OpenCV? I'm using C++, the image is stored in cv::Mat variable.
I'm showing some of my efforts so far: I tried this code from another stackoverflow link. But every time I re-run the code the value in Hexadecimal changed! For example once its 00CD5D7C, in next run it is 00C09D7C.
cv::Mat img_rgb = cv::imread("img6.jpg");
Point3_<uchar>* p = img_rgb.ptr<Point3_<uchar> >(10,10);
p->x; //B
p->y; //G
p->z; //R
std::cout<<p;
In another try I used this code from another answer. Here output is always -858993460.
img_rgb.at<cv::Vec3b>(10,10);
img_rgb.at<cv::Vec3b>(10,10)[0] = newval[0];
img_rgb.at<cv::Vec3b>(10,10)[1] = newval[1];
img_rgb.at<cv::Vec3b>(10,10)[2] = newval[2];
cout<<newval[0]; //For cout<<newval[1]; cout<<newval[2]; the result is still same
NOTE: I used (10,10) as a test to get the RGB, my target is get the RGB values if the whole image!
Since you are loading a color image (of type CV_8UC3), you need to access its elements with .at<Vec3b>(row, col). The elements are in BGR order:
Mat img_bgr = imread("path_to_img");
for(int r = 0; r < img_bgr.rows; ++r) {
for(int c = 0; c < img_bgr.cols; ++c) {
std::cout << "Pixel at position (x, y) : (" << c << ", " << r << ") =" <<
img_bgr.at<Vec3b>(r,c) << std::endl;
}
}
You can also simplify using Mat3b (aka Mat_<Vec3b>), so you don't need to use the .at function, but using directly the parenthesis:
Mat3b img_bgr = imread("path_to_img");
for(int r = 0; r < img_bgr.rows; ++r) {
for(int c = 0; c < img_bgr.cols; ++c) {
std::cout << "Pixel at position (x, y) : (" << c << ", " << r << ") =" <<
img_bgr(r,c) << std::endl;
}
}
To get each single channel, you can easily do:
Vec3b pixel = img_bgr(r,c); // or img_bgr.at<Vec3b>(r,c)
uchar blue = pixel[0];
uchar green = pixel[1];
uchar red = pixel[2];

How to access the RGB values in Opencv?

I am confused about the use of number of channels.
Which one is correct of the following?
// roi is the image matrix
for(int i = 0; i < roi.rows; i++)
{
for(int j = 0; j < roi.cols; j+=roi.channels())
{
int b = roi.at<cv::Vec3b>(i,j)[0];
int g = roi.at<cv::Vec3b>(i,j)[1];
int r = roi.at<cv::Vec3b>(i,j)[2];
cout << r << " " << g << " " << b << endl ;
}
}
Or,
for(int i = 0; i < roi.rows; i++)
{
for(int j = 0; j < roi.cols; j++)
{
int b = roi.at<cv::Vec3b>(i,j)[0];
int g = roi.at<cv::Vec3b>(i,j)[1];
int r = roi.at<cv::Vec3b>(i,j)[2];
cout << r << " " << g << " " << b << endl ;
}
}
the second one is correct,
the rows and cols inside the Mat represents the number of pixels,
while the channel has nothing to do with the rows and cols number.
and CV use BGR by default, so assuming the Mat is not converted to RGB then the code is correct
reference, personal experience, OpenCV docs
A quicker way to get color components from an image is to have the image represented as an IplImage structure and then make use of the pixel size and number of channels to iterate through it using pointer arithmetic.
For example, if you know that your image is a 3-channel image with 1 byte per pixel and its format is BGR (the default in OpenCV), the following code will get access to its components:
(In the following code, img is of type IplImage.)
for (int y = 0; y < img->height; y++) {
for(int x = 0; x < img->width; x++) {
uchar *blue = ((uchar*)(img->imageData + img->widthStep*y))[x*3];
uchar *green = ((uchar*)(img->imageData + img->widthStep*y))[x*3+1];
uchar *red = ((uchar*)(img->imageData + img->widthStep*y))[x*3+2];
}
}
For a more flexible approach, you can use the CV_IMAGE_ELEM macro defined in types_c.h:
/* get reference to pixel at (col,row),
for multi-channel images (col) should be multiplied by number of channels */
#define CV_IMAGE_ELEM( image, elemtype, row, col ) \
(((elemtype*)((image)->imageData + (image)->widthStep*(row)))[(col)])
I guess the 2nd one is correct, nevertheless it is very time consuming to get the data like that.
A quicker method would be to use the IplImage* data structure and increment the address pointed with the size of the data contained in roi...

opencv, accessing element for downsampling but white window appear

i'm learning c++ api of opencv, and for a simple approach i've started with try to downsample image (ok i know that there is pyrDown with gaussian resampling but it's for learning how to access element in Mat class)
this is my code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#define original_window "original"
#define manual_window "manual"
using namespace cv;
using namespace std;
Mat img, manual;
void downsample(Mat src, Mat &dst, const Size& s) {
float factor = src.rows/(float)s.width;
Mat_<Vec3f> _dst = Mat(s, src.type());
Mat_<Vec3f> _src = src;
for(int i=0; i<src.cols; i+=factor) {
int _i = i/factor;
for(int j=0; j<src.rows; j+=factor) {
int _j = j/factor;
_dst (_j, _i) = _src(j,i);
}
}
cout << "downsample image size: " << _dst.rows << " " << _dst.cols << endl;
dst = Mat(_dst);
}
int main(int /*argc*/, char** /*argv*/) {
img = imread("lena.jpg");
cout << "original image size: " << img.rows << " " << img.cols << endl;
downsample(img, manual, Size(img.cols/2, img.rows/2));
namedWindow(original_window, CV_WINDOW_AUTOSIZE);
namedWindow(manual_window, CV_WINDOW_AUTOSIZE);
while( true )
{
char c = (char)waitKey(10);
if( c == 27 )
{ break; }
imshow( original_window, img );
imshow( manual_window, manual );
}
return 0;
}
now, i'm doing a downsampling in a fool way: i'm just deleting elements. and i'm try to use c++ api with Mat_.
in manual window i get a white window, and i don't understand why. event if i try to cout manual i'seeing different values.. what's wrong with this piece of code?
EDIT 1
i've found a solution:
dst.convertTo(dst, src.type()); // in this particular case: src.type() == CV_8UC3
at the end of downsample()
now my question is: why that? i declare Mat(s, src.type()); why it is modified?
EDIT 2
if i use #go4sri answer with this line
_dst (_j, _i) = src.at<Vec3f>(j, i);
i get this output:
i really does not understand why..
The way to access an element in OpenCV's Mat is as follows:
for a single channel matrix(tmp)
Matrix_Name.at<dataType>(row, col)
For a three channel matrix( as is the case for a color image), you will need to use the Vec3b/Vec3f type depending upon if yours is a unsigned char/float matrix.
As yours is a unsigned char 3Dimensional matrix:
you will have to access it as src.at<Vec3b>(i, j)
Your downsample should have been:
void downsample(const Mat& src, Mat &dst, const Size& s) {
float factor = src.rows/(float)s.height;
Mat _dst = Mat(s, src.type());
for(int i=0; i < src.cols; i += factor) {
int _i = i/factor;
for(int j=0; j<src.rows; j+=factor) {
int _j = j/factor;
_dst.at<Vec3b> (_j, _i) = src.at<Vec3b>(j, i);
}
}
cout << "downsample image size: " << _dst.rows << " " << _dst.cols << endl;
dst = Mat(_dst);
}

assignment error with Mat OpenCv

I am working with OpenCV and C++ for a project and I found the following problem: after initializing a mat with the following statement
Mat or_mat=Mat(img->height,img->width,CV_32FC1);
check the following value
or_mat.at <float> (i, j) = atan (fy / fx) / 2 +1.5707963;
After completing returning the mat for the output of the function but when I go to read there are many values ​​that do not correspond to the output. Precise in incorrect values ​​for the I-4.31602e +008 is inserted and if I make a cout the value of the expression is correct. What could be the error??
relevant Code:
Mat or_mat=Mat(img->height,img->width,CV_32FC1);
to angle
if(fx > 0){
or_mat.at<float>(i,j) = atan(fy/fx)/2+1.5707963;
}
else if(fx<0 && fy >0){
or_mat.at<float>(i,j) = atan(fy/fx)/2+3.1415926;
}
else if(fx<0 && fy <0){
or_mat.at<float>(i,j) = atan(fy/fx)/2;
}
else if(fy!=0 && fx==0){
or_mat.at<float>(i,j) = 1.5707963;
}
I have to calculate the local orientation of the fingerprint image, the following code I have omitted several statements and calculations that do not have errors.
I would triple check that you are indexing correctly. The following code shows my initialising a matrix full of zeros, and then filling it with some float using at .at operator. It compiles and runs nicely:
int main()
{
int height = 10;
int width = 3;
// Initialise or_mat to with every element set to zero
cv::Mat or_mat = cv::Mat::zeros(height, width, CV_32FC1);
std::cout << "Original or_mat:\n" << or_mat << std::endl;
// Loop through and set each element equal to some float
float value = 10.254;
for (int i = 0; i < or_mat.rows; ++i)
{
for (int j = 0; j < or_mat.cols; ++j)
{
or_mat.at<float>(i,j) = value;
}
}
std::cout << "Final or_mat:\n" << or_mat << std::endl;
return 0;
}