opencv iplimage binarization - c++

I have an IplImage and I want to binarize (without using cvThreshold function) it using this following code:
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
static IplImage* img ;
img = cvLoadImage ("c:\\Mytest.jpg");
for(int i=0;i<img->height;i++)
{
for(int j=0;j<img->width;j++)
{
if (img->imageData[i*img->widthStep+j]<=10)
((uchar *)img->imageData)[i*img->widthStep+j]=255;
else
((uchar *)img->imageData)[i*img->widthStep+j]=0;
}
}
cvShowImage("After",img);
waitKey(0);
};
but this code affected only part of the image, like this:

see, it's a breeze with c++:
Mat img = imread("c:\\Mytest.jpg", 0); // load grayscale
Mat thresh = ( im <= 10 ); // that's it already!
imshow("After",img);
waitKey(0);
also look at threshold()

Related

OpenCV: Unable to run Canny edge detection in C++

I have written a simple code to perform canny edge detection on a live stream. The code is as shown below,
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int lowThreshold=0;
int const max_lowThreshold = 100;
int kernel_size = 3;
int ratio = 3;
Mat img;
Mat display;
void CannyThreshold()
{
cvtColor(img, display, COLOR_RGB2GRAY);
// GaussianBlur(display,display,Size(7,7),3,3);
GaussianBlur(display, display, Size(1, 1), 1,1);
printf("%d\n",lowThreshold);
Canny(display,display,lowThreshold,3);
imshow("Canny",display);
}
int main()
{
VideoCapture cap(0);
namedWindow("Canny");
createTrackbar("Min Threshold: ","Canny",&lowThreshold,max_lowThreshold);
while(1)
{
cap.read(img);
int ret = waitKey(1);
CannyThreshold();
if(ret == 'q')
break;
}
cap.release();
return 0;
}
I get the following run-time error when I run the code. (I'm using OpenCV 4)
error: (-215:Assertion failed) ksize.width > 0 && ksize.width % 2 == 1 && ksize.height > 0 && ksize.height % 2 == 1 in function 'createGaussianKernels'
Any suggestions on how I can solve this error?
The issue is GaussianBlur cant accept kernel size of 1. Correct it to 3x3 or 5x5 in your code as follows
#include <opencv2/core/utility.hpp>
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
VideoCapture cap;
Mat frame;
Mat image; // from cap to image
Mat src_gray;
Mat dst;
Mat detected_edges;
const String window_name = "Canny Edge Detector - VideoCapture";
int lowThreshold = 10;
const int max_lowThreshold = 100;
const int ratio = 3;
const int kernel_size = 3;
int FPS;
int frame_width, frame_height;
int camNum = 0;
cap.open(camNum);
if (!cap.isOpened())
{
cout << "***Could not initialize capturing...***\n";
cout << "Current parameter's value: \n";
return -1;
}
FPS = cap.get(CAP_PROP_FPS);
frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
dst.create(frame_width, frame_height, CV_8UC3);
//cout << CV_8UC3;
while (true)
{
cap >> frame;
if (frame.empty())
break;
frame.copyTo(image);
// Convert the image to grayscale
cvtColor(image, src_gray, COLOR_BGR2GRAY);
//![reduce_noise]
/// Reduce noise with a kernel 3x3
blur(src_gray, detected_edges, Size(3, 3));
//![reduce_noise]
//![canny]
/// Canny detector
Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
//![canny]
/// Using Canny's output as a mask, we display our result
//![fill]
dst = Scalar::all(0);
//![fill]
//![copyto]
image.copyTo(dst, detected_edges);
//![copyto]
//![display]
imshow(window_name, dst);
if (waitKey(1000 / FPS) >= 0)
break;
}
return 0;
}

How can I use fastNlMeansDenoisingColored() in C++?

I am new in learning OpenCV. While I was searching for good algorithm to remove noise I found this Function but I have no idea how to use it, so is this function available in OpenCV libraries? If yes, how can I use it.
void fastNlMeansDenoisingColored(InputArray src, OutputArray dst, float h=3, float hColor=3, int templateWindowSize=7, int searchWindowSize=21 )
This is from the opencv documentation. Therefore try using that function.
A snippet of code I found online may be able to help you:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main() {
Mat img = imread("a.jpg");
if (!img.data) {
cout << "No image found" << endl;
return -1;
}
// first copy the image
Mat img_gray = img.clone();
cvtColor(img, img_gray, CV_RGB2GRAY);
Mat img1;
cv::fastNlMeansDenoising(img_gray, img1, 3.0, 7, 21);
imshow("img1", img1);
waitKey();
return 0;
}
Hope it helps!

binarize image as tif

I have found a program which can binarize an image and make the image monochrome
OpenCV Adaptive Threshold OCR
I have copy/pasted the code
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdarg.h>
#include "/usr/include/opencv2/opencv.hpp"
#include "fstream"
#include "iostream"
using namespace std;
using namespace cv;
void CalcBlockMeanVariance(Mat& Img,Mat& Res,float blockSide=21) // blockSide - the parameter (set greater for larger font on image)
{
Mat I;
Img.convertTo(I,CV_32FC1);
Res=Mat::zeros(Img.rows/blockSide,Img.cols/blockSide,CV_32FC1);
Mat inpaintmask;
Mat patch;
Mat smallImg;
Scalar m,s;
for(int i=0;i<Img.rows-blockSide;i+=blockSide)
{
for (int j=0;j<Img.cols-blockSide;j+=blockSide)
{
patch=I(Range(i,i+blockSide+1),Range(j,j+blockSide+1));
cv::meanStdDev(patch,m,s);
if(s[0]>0.01) // Thresholding parameter (set smaller for lower contrast image)
{
Res.at<float>(i/blockSide,j/blockSide)=m[0];
}else
{
Res.at<float>(i/blockSide,j/blockSide)=0;
}
}
}
cv::resize(I,smallImg,Res.size());
cv::threshold(Res,inpaintmask,0.02,1.0,cv::THRESH_BINARY);
Mat inpainted;
smallImg.convertTo(smallImg,CV_8UC1,255);
inpaintmask.convertTo(inpaintmask,CV_8UC1);
inpaint(smallImg, inpaintmask, inpainted, 5, INPAINT_TELEA);
cv::resize(inpainted,Res,Img.size());
Res.convertTo(Res,CV_32FC1,1.0/255.0);
}
int main( int argc, char** argv )
{
namedWindow("Img");
namedWindow("Edges");
//Mat Img=imread("D:\\ImagesForTest\\BookPage.JPG",0);
Mat Img=imread("Test2.JPG",0);
Mat res;
Img.convertTo(Img,CV_32FC1,1.0/255.0);
CalcBlockMeanVariance(Img,res);
res=1.0-res;
res=Img+res;
imshow("Img",Img);
cv::threshold(res,res,0.85,1,cv::THRESH_BINARY);
cv::resize(res,res,cv::Size(res.cols/2,res.rows/2));
imwrite("result.jpg",res*255);
imshow("Edges",res);
waitKey(0);
return 0;
}
compile
g++ binarize.cpp `pkg-config opencv --cflags --libs`
run
./a.out
error
(Img:27277): Gtk-WARNING **: cannot open display:
update
int main( int argc, char** argv )
{
namedWindow("Img");
namedWindow("Edges");
//Mat Img=imread("D:\\ImagesForTest\\BookPage.JPG",0);
Mat Img=imread("Test2.JPG",0);
Mat res;
Img.convertTo(Img,CV_32FC1,1.0/255.0);
CalcBlockMeanVariance(Img,res);
res=1.0-res;
res=Img+res;
imshow("Img",Img);
cv::threshold(res,res,0.85,1,cv::THRESH_BINARY);
cv::resize(res,res,cv::Size(res.cols/2,res.rows/2));
imwrite("result.tif",res*255);
imshow("Edges",res);
waitKey(0);
return 0;
}
Just built this code on ubuntu 14.03 x64, using the same command line as you used, no any messages, runs fine.
I think warning related to your system environment.

opencv write into a file the pixel values of binary image

I would like to ask question about on how to export/write all the pixel values into a txt file or other format that can be opened by notepad for example. Below the program.
Thanks, HB
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
#include <stdlib.h>
#include<fstream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
IplImage *img = cvLoadImage("MyImg.png");
CvMat *mat = cvCreateMat(img->height,img->width,CV_32FC3 );
cvConvert( img, mat );
outFile.open("MyFile.txt");
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
/// Get the (i,j) pixel value
CvScalar scal = cvGet2D( mat,j,i);
printf( "(%.f,%.f,%.f)",scal.val[0], scal.val[1],scal.val[2] );
}
printf("\n");
}
waitKey(1);
return 0;
}
The class Mat of the new OpenCV C++ API is preferred over IplImage because it simplifies your code: read more about the class Mat. For more information about loading an image, you could read Load, Modify, and Save an Image.
In order to write a text file using C++, you could use the class ofstream
Here is the source code.
#include <opencv2/opencv.hpp>
using namespace cv;
#include <fstream>
using namespace std;
int main( int argc, char** argv )
{
Mat colorImage = imread("MyImg.png");
// First convert the image to grayscale.
Mat grayImage;
cvtColor(colorImage, grayImage, CV_RGB2GRAY);
// Then apply thresholding to make it binary.
Mat binaryImage(grayImage.size(), grayImage.type());
threshold(grayImage, binaryImage, 128, 255, CV_THRESH_BINARY);
// Open the file in write mode.
ofstream outputFile;
outputFile.open("MyFile.txt");
// Iterate through pixels.
for (int r = 0; r < binaryImage.rows; r++)
{
for (int c = 0; c < binaryImage.cols; c++)
{
int pixel = binaryImage.at<uchar>(r,c);
outputFile << pixel << '\t';
}
outputFile << endl;
}
// Close the file.
outputFile.close();
return 0;
}

c++ How to put the video sequence into a vector<Mat> in OpenCV?

I am a new learner in c++. I read a video and I want to save the image sequence of the video into a vector called vector frame. The following is my code, please help me correct it if someone could, thank you a lot!
#include <cv.h>
#include <highgui.h>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture("/home/P1030.MOV");
int totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
vector<Mat> frame;
namedWindow("Display", WINDOW_AUTOSIZE);
for(int i=0; i < totalFrameNumber; i++)
{
frame.push_back(Mat());
imshow("Display", frame);
}
return 0;
}
You can do it as follows, although it is not recommended to load whole video in the memory at a single time.
int main()
{
VideoCapture capture("/home/P1030.MOV");
int totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
Mat currentFrame;
vector<Mat> frames;
namedWindow("Display", WINDOW_AUTOSIZE);
for(int i=0; i < totalFrameNumber; i++)
{
capture>>currentFrame;
frames.push_back(currentFrame.clone());
imshow("Display", currentFrame);
waitKey(10);
}
return 0;
}