How can I use fastNlMeansDenoisingColored() in C++? - 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!

Related

can we apply threshold to single component of color space model like RGB and LAB?

trying to apply the Otsu threshold to single component "L" of LAB color space. But I can not figure out, how to specify it in OpenCV syntactically.
C++ code splits the Lab image into the separate channels.
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")
int main(void)
{
Mat img = imread("star.png", 1);
if (img.empty())
{
cout << "Could not read image file." << endl;
return 1;
}
Mat Lab;
Mat Lab_channels[3];
cvtColor(img, Lab, COLOR_BGR2Lab);
split(Lab, Lab_channels);
threshold(Lab_channels[0], Lab_channels[0], 127, 255, THRESH_OTSU);
return 0;
}
This C++ code uses extract channel to get just the first channel (channel 0).
#include <iostream>
using namespace std;
#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")
int main(void)
{
Mat img = imread("star.png", 1);
if (img.empty())
{
cout << "Could not read image file." << endl;
return 1;
}
Mat Lab;
Mat Lab_channel_0;
cvtColor(img, Lab, COLOR_BGR2Lab);
extractChannel(Lab, Lab_channel_0, 0);
threshold(Lab_channel_0, Lab_channel_0, 127, 255, THRESH_OTSU);
return 0;
}
Try this.

After adding SurfFeatureDetector, program giving error

I am using opencv 3.3.0
my code is
#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include <fstream>
#include <string>
#include <vector>
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int main(int argc, char** argv)
{
Mat img1;
img1 = imread("img/img1.jpg", IMREAD_GRAYSCALE);
if (img1.empty())
{
cout << "Could not open or find the image" << endl;
return -1;
}
Ptr<SurfFeatureDetector> detector = SurfFeatureDetector::create(400);
vector<KeyPoint> keypoints;
detector->detect(img1, keypoints);
imshow("img1", img1);
waitKey(0);
return 0;
}
I am new to C++ and opencv. The code work well without surf part.
error
OpenCV Error: Assertion failed (TlsSetValue(tlsKey, pData) == TRUE) in cv::TlsAbstraction::SetData, file C:\Users\Darshana\Documents\opencv\opencv-3.3.0\modules\core\src\system.cpp, line 1270
I have tried with Ptr<SURF> detector = SURF::create(400); as well.
UPDATE
I couldn't figure out a solution for this. Maybe a setup issue or library issue. So I just move to opencv 2.4.13 and now everything works fine. I had to change the above code to work with v2.4.13 though.
You haven't set parameter for SurfFeatureDetector. I think you should try this instead:
int minHessian = 400;
SurfFeatureDetector detector(minHessian);
vector<KeyPoint> keypoints;
detector.detect(img1, keypoints);

Access Violation Exception Thrown OpenCV

Im working on a simple face detector using Visual Studio 2017 with OpenCV 3.2.0 and haarcascades.
Whenever i run this, i get an error saying there's an exception thrown right after i create the first point. Access violation error. Any help would be greatly appreciated as I do not see any error in the code.
#include <opencv2/opencv.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
CascadeClassifier face_cascade;
CascadeClassifier eye_cascade;
face_cascade.load("haarcascade_frontalface_alt.xml");
eye_cascade.load("haarcascade_eye_tree_eyeglasses.xml");
VideoCapture cap(0);
Mat frame;
while (cap.read(frame))
{
Mat frame_gray;
std::vector <Rect> faces;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
face_cascade.detectMultiScale(frame_gray,faces, 1.1, 2,0,Size(30,30),Size(300,300));
for (size_t i = 0; i < faces.size();i++)
{
Point first(faces[i].x, faces[i].y);
Point second(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
rectangle(frame,second,first, cvScalar(255, 0, 0), 1, 8, 0);
}
imshow("Test", frame_gray);
waitKey(30);
}
return 0;
}

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 iplimage binarization

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()