Recognize type of noise - visual-c++

What is kind of this noise?
Link :Image
How To remove it? I apply median and fastNlMeansDenoisingColored() functions but no improvement.
Sample programm:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img,gray,DENO, im_gray, img_bw, img_final;
img = imread("C:/Users/Opencv/Downloads/1.bmp",1);
cvtColor(img,im_gray,CV_RGB2GRAY);
adaptiveThreshold(im_gray, img_bw, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 105, 1);
dilate(img_bw, img_final, Mat(), Point(-1, -1), 2, 1, 1);
imshow("afa",img_final);
waitkay(0);
return 0;
}
Visual Studio 2012,C++, OpenCV 3.0.0
Regards

I write these codes :
ressu3 = zeros(m,n);
for i=2:m-1
for j=2:n-1
ressu3(i,j)=1/55* median ([I(i-1,j-1),I(i-1,j+1),I(i-1,j),I(i+1,j- 1),I(i+1,j+1),I(i+1,j)]);
end
end
The codes is writen in matlab(for test).
Is it directional median filter?

Try below code for removing Noise,
cv::Mat mainMat1 = cv::imread("input_image_path");
cv::Mat dstMat1, medianMat1, finalDest1;
bilateralFilter(mainMat1, dstMat1, 19, 19 * 2, 19 / 2);
addWeighted(mainMat1, 0.40, dstMat1, 0.60, 0, finalDest1, -1);
medianBlur(mainMat1, medianMat1, 17);
cv::Mat dstination1;
cv::fastNlMeansDenoisingColored(mainMat1,dstination1,10,10,7,21);
Hope This will help.

Related

Wrong output with scanf function

so this is supposedly not a difficult question, but I've been getting this problem a few times when running my code in VS code. I am trying to separate the alphabets and numbers from the string, and I have used the method as follows (in my code) according to what is taught in the book. However, despite having the program running, the output is wrong.
Here is my code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height = 0;
char wunit[] = "";
char hunit[] = "";
printf("Enter the body weight: ");
scanf("%d%s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%s",&height,hunit);
printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
The thing is,if I type in 20lb for weight, and 30mt for height, what happens is that it gives the output: 20,t,30,mt; which generates this weird ‘t’ instead of lb, and I have no idea why this is the case.
Similarly, when I type 30kg for weight, and 20cm for height. It generates this weird output:30,m,0, cm. The kg becomes a 'm' and the 20 is now a '0'!? Why is that the case? The expected output would be 30,kg,20,cm
I tried simply replacing the strings, but that doesn't solve the problem fundamentally. For instance, (considering when my user puts logical inputs like lb or kg for weight), I tried this substitution and it appears to work, but doesn't fix the issue of making 20 -> 0
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height= 0;
char wunit[] = "";
char hunit[] = "";
char wunit2[] = "lb";
char dummy[] = "t";
printf("Enter the body weight: ");
scanf("%d%s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%s",&height,hunit);
if (strcmp(wunit,dummy)==0){
printf("%d,%s,%d,%s\n", weight, wunit2, height, hunit);
}
//printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
I've also tried running it in codecollab, and it shows this error of "stack smashing detected" after I run it a few times, which got me more confused, what has it to do with this?
Thanks in advance.
wunit is an array of size 1 (it is initialized to "", which in chars looks like {'\0'}). What happens when you try to put lots of characters (say, "lb", which is {'l', 'b', '\0'}) into a memory location that is smaller than it should be?
scanf happily writes as many bytes as needed, smashing anything in its way ("stack-smashing", because wunit and all those local variables are stored on the stack). Try to give scanf more space, say using
char wunit[10] = "";
And never ever use "%s" directly. Limit the maximum of characters that you will allow scanf to place, for example using "%9s" to ensure that at most 9 characters + terminator (10 total) will be read.
This works for me:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height = 0;
char wunit[10] = "";
char hunit[10] = "";
printf("Enter the body weight: ");
scanf("%d%9s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%9s",&height,hunit);
printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
Note: scanf with %s is rightfully considered very dangerous. See https://stackoverflow.com/a/2430310/15472

Transparency on one face of cube OpenScreenGraph

I have imported object (Cube) from 3dsMax in my OSG project in VisualStudio. But I can't find out how to make transparent only one face of this imported cube. this is my code:
#include <osgViewer/Viewer>
#include <iostream>
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/StateSet>
#include <osg/StateAttribute>
#include <osg/CullFace>
#include <osg/Point>
#include <osg/Light>
#include <osg/LightSource>
#include <osg/BlendFunc>
#include <osg/Material>
#include <osg/PolygonMode>
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cube.3ds"); //Importing model
osg::StateSet* state2 = model->getOrCreateStateSet(); //Creating material
osg::ref_ptr<osg::Material> mat2 = new osg::Material;
mat2->setAlpha(osg::Material::FRONT_AND_BACK, 0.1); //Making alpha channel
state2->setAttributeAndModes( mat2.get() ,
osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
osg::BlendFunc* bf = new //Blending
osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
osg::BlendFunc::ONE_MINUS_DST_COLOR );
state2->setAttributeAndModes(bf);
root->addChild(model.get());
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
viewer.setUpViewOnSingleScreen(0);
return viewer.run();
}
This is my source with just imported file. I've tried implement transparency with multiple passes but have no success.
Is there any method how could i make it ?
The code in the question does make the model transparent. For example, with the cessna model from the OSG data package:
Adding two more models, a box and a sphere, where the box also has blending:
We see that blending is working. If you add another model but it isn't displayed, then probably the plane is being rendered before the other models. If the plane happens to be in front of the other models, even if the plane is transparent, you won't see them; as they don't pass the depth test.
Adding
cessna->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
cessna->getStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
forces the cessna to be rendered after the opaque models.
Also, note that if you provide a blending function, then you don't need to call
cessna->getStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
Let's see another scene, where the box is behind the plane and we haven't set the rendering hint:
And now with the rendering hint active:

How to calculate the intersection of a line segment and circle with CGAL

I've been banging my head against a wall trying to understand how to use CGAL's Circular Kernel to calculate the intersection(s) between a line segment (Line_Arc_2) and a Circle (Circle_2). Unfortunately there isn't much in the way of example code for the Circular Kernel, and I'm not finding the reference manual much help.
Here is code that I thought would work, but right now it won't even compile (Mac OS 10.9 using the latest system compiler):
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2)>::type Res;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<Res> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}
I get an error on the result_of line: "error: no type named 'result_type' in...", and a second error that "no viable overloaded '='" is available for the intersection line.
Also, since this would probably be the follow up question once this is working: how do I actually get at the intersection points that are put in the vector? CGAL's documentation suggests to me "result" should contain pairs of a Circular_arc_point_2 and an unsigned int representing its multiplicity. Is this what I will actually get in this case? More generally, does anyone know a good tutorial for using the Circular Kernel and Spherical Kernel intersection routines?
Thanks!
So it seems that result_of doesn't work here, despite being suggested in the CGAL reference manual for the CircularKernel's intersection function.
Here is a different version that seems to work and can properly handle the output:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/iterator.h>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> IsectOutput;
using namespace std;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1.0,0.0), Pt2(0.0,1.0), Pt2(-1.0, 0.0));
LineArc2 l = LineArc2( Pt2(0.0,-2.0), Pt2(0.0,2.0) );
std::vector<IsectOutput> output;
typedef CGAL::Dispatch_output_iterator< CGAL::cpp11::tuple<IsectOutput>,
CGAL::cpp0x::tuple< std::back_insert_iterator<std::vector<IsectOutput> > > > Dispatcher;
Dispatcher disp = CGAL::dispatch_output<IsectOutput>( std::back_inserter(output) );
CGAL::intersection(l, c, disp);
cout << output.size() << endl;
for( const auto& v : output ){
cout << "Point: (" << CGAL::to_double( v.first.x() ) << ", " << CGAL::to_double( v.first.y() ) << "), Mult: "
<< v.second << std::endl;
}
return 0;
}
result_of is working but the operator you are asking for does not exist, you are missing the output iterator.
However, I agree the doc is misleading. I'll try to fix it.
The following code is working fine:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef boost::variant<std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> > InterRes;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2,std::back_insert_iterator<std::vector<InterRes> >)>::type Res;
int main(){
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<InterRes> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}

show first frame in the video using open cv?

I am working on stereo vision.Here i have two stereo videos.I wanted to do following things
1]extract the first frame from the video .
2] convert the frame to gray channel(so that i can apply SURF features)
3]display the first frame (getting error in the code given below)
4]store the video with small size(currently it is 1600*1200)
can anyone give suggestion how to do?
my code is given below(written in c++)
#include <stdio.h>
#include <iostream>
#include <vector>
#include <time.h>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include "opencv2\features2d\features2d.hpp"
#include "opencv2\nonfree\features2d.hpp"
#include "opencv2\nonfree\nonfree.hpp"
#include "opencv2\flann\flann.hpp"
#include "opencv2\contrib\contrib.hpp"
#include <opencv2\calib3d\calib3d.hpp>
#include <opencv2\gpu\gpumat.hpp>
#pragma comment (lib, "opencv_core243d")
#pragma comment (lib, "opencv_highgui243d")
#pragma comment (lib, "opencv_imgproc243d")
#pragma comment (lib, "opencv_features2d243d")
#pragma comment (lib, "opencv_nonfree243d")
#pragma comment (lib, "opencv_flann243d")
#pragma comment (lib, "opencv_contrib243d")
#pragma comment (lib, "opencv_calib3d243d")
#pragma comment (lib, "opencv_gpu243d")
using namespace std;
using namespace cv;
int main(int argc, char**argv)
{
clock_t start_time=clock();
long lframecount=0;
//load the videos
VideoCapture capture1(argv[1]);
VideoCapture capture2(argv[2]);
cout << argv[1] << endl;
cout << argv[2] << endl;
if(!capture1.isOpened()||!capture2.isOpened())
{
cout<<"cant load stereo video";
return -1;
}
Mat frame1,frame2;
capture1>>frame1;
capture2>>frame2;
int num_rows=frame1.rows;
int num_cols=frame1.cols;
std::cout<<"number of rows and colums"<<num_rows<<":"<<num_cols<<std::endl;
int output_rows=num_rows/4;
int output_cols=num_cols/4;
long framecount1= capture1.get(CV_CAP_PROP_FRAME_COUNT);
long framecount2= capture2.get(CV_CAP_PROP_FRAME_COUNT);
VideoWriter write;
write.open("E:\\Vipil\\open_cv_learning\\video\\new22.avi",CV_FOURCC('D','I','V','X'), 30,cv::Size(output_rows,output_cols) ,true);
if (!write.isOpened())
{
std::cout << "cant open output video";
return -1;
}
namedWindow("depthmap",cv::WINDOW_NORMAL);
cv::Point tex(570, 50);
cv::Mat image1;
cv::Mat image2;
for(long i=1;i<6;i++)
{
lframecount++;
int number=lframecount;
string frame;
std::ostringstream convert;
convert<<number;
frame = convert.str();
capture1>> frame1;
capture2>> frame2;
cv::cvtColor(frame1,image1,CV_RGB2GRAY);
cv::cvtColor(frame2,image2,CV_RGB2GRAY);
imshow("image1",frame1);
//cout<<lframecount<<"\n";
}
You will need to use waitKey(30) or some delay to display image correctly.
Also to resize your frame you can simply use OpenCV resize() function.
Here is the reference
http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html

Bus error opening and mmap'ing a file

I want to create a file and map it into memory. I think that my code will work but when I run it I'm getting a "bus error". I searched google but I'm not sure how to fix the problem. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
int main(void)
{
int file_fd,page_size;
char buffer[10]="perfect";
char *map;
file_fd=open("/tmp/test.txt",O_RDWR | O_CREAT | O_TRUNC ,(mode_t)0600);
if(file_fd == -1)
{
perror("open");
return 2;
}
page_size = getpagesize();
map = mmap(0,page_size,PROT_READ | PROT_WRITE,MAP_SHARED,file_fd,page_size);
if(map == MAP_FAILED)
{
perror("mmap");
return 3;
}
strcpy(map, buffer);
munmap(map, page_size);
close(file_fd);
return 0;
}
You are creating a new zero sized file, you can't extend the file size with mmap. You'll get a bus error when you try to write outside the content of the file.
Use e.g. fallocate() on the file descriptor to allocate room in the file.
Note that you're also passing the page_size as the offset to mmap, which doesn't seem to make much sense in your example, you'll have to first extend the file to pagesize + strlen(buffer) + 1 if you want to write buf at that location. More likely you want to start at the beginning of the file, so pass 0 as the last argument to mmap.

Resources