วันศุกร์ที่ 15 ธันวาคม พ.ศ. 2560

C# Search , Add , Delete DataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        DataTable table;
        DataTable table1;
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var item = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
            table1.Rows.Add(item.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            table = new DataTable();
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Rows.Add(1,"Mee");
            table.Rows.Add(2, "Yha");
            dataGridView1.DataSource = table;

            table1 = new DataTable();
            table1.Columns.Add("Name", typeof(string));
            dataGridView2.DataSource = table1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridView2.CurrentCell.RowIndex;
                dataGridView2.Rows.RemoveAt(rowIndex);
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyData == Keys.Delete)
            {
                int rowIndex = dataGridView2.CurrentCell.RowIndex;
                    dataGridView2.Rows.RemoveAt(rowIndex);
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == (char)13)
            {
                DataView dv = table.DefaultView;
                dv.RowFilter = string.Format("Name like '%{0}%'" , textBox1.Text);
                dataGridView1.DataSource = dv.ToTable();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataView dv = table.DefaultView;
            dv.RowFilter = string.Format("Name like '%{0}%'", textBox1.Text);
            dataGridView1.DataSource = dv.ToTable();
        }
    }
}

วันศุกร์ที่ 24 พฤศจิกายน พ.ศ. 2560

C# Binary Search

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Linkedlist
{
    class Program
    {
        static public int[] numsort(int[] x)
        {
            int[] a = x;

            for (int i = 0; i < a.Length; i++)
            {
                for (int j = 0; j < a.Length - 1; j++)
                {
                    if (a[j] > a[j + 1])
                    {
                        int temp;
                        temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            return a;
        }
        static public int binarySerach(int[] x,int find)
        {
            int left = 0;
            int right = x.Length-1;
            int mid;
            while(left <= right)
            {
                mid = (left + right) / 2;
                if (find == x[mid])
                    return mid;
                else if (find < x[mid])
                {
                    right = mid - 1;
                }
                else
                    left = mid + 1;
            }
            return -1;
        }
        static public void output(int[] x)
        {
            for(int i=0;i<x.Length;i++)
            {
                Console.Write(x[i]+" ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            Random r = new Random();
            int find = 5;
            int[] x = new int[10];
            for(int i=0;i<10;i++)
            {
                x[i] = r.Next(10) + 1;
            }
            numsort(x);
            output(x);
            int c = binarySerach(x, find);
            Console.WriteLine("Find : " + find + " Index : " + (c+1));
         

            Console.ReadLine();
        }   
    }

}

วันศุกร์ที่ 3 พฤศจิกายน พ.ศ. 2560

C# sequential Search

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LinkedList
{
    public partial class Form1 : Form
    {
        int n = 100;
        int[] a;
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            a = new int[n];
            Random x = new Random();
            textBox1.Text = "";
            for(int i=0;i<n;i++)
            {
                a[i] = x.Next(100) + 1;
                textBox1.Text = textBox1.Text + "[" +i.ToString() + "]"+a[i].ToString() + " , ";
            }
        }

        private int sequentialsearch(int k)
        {
            int i=0;
            while(i<n && a[i] !=k)
            {
                i++;
            }
            if (i < n)
                return i;
            else
                return -1;             
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int k = Convert.ToInt32(textBox2.Text);
            textBox3.Text = sequentialsearch(k).ToString();
        }


    }

}

C# LinkedList

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LinkedList
{
    public partial class Form1 : Form
    {
        singlrLinkedList sll;
        public Form1()
        {
            InitializeComponent();
            startup();
        }
        public void startup()
        {
            sll = new LinkedList.singlrLinkedList();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            sll.addLinkedList(textBox1.Text, Int32.Parse(textBox2.Text.ToString()));
         
            textBox1.Clear();
            textBox2.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox3.Text = sll.showall();
   
        }

        private void button3_Click(object sender, EventArgs e)
        {
            sll.deletepos(Convert.ToInt32(textBox4.Text));
            textBox3.Text = sll.showall();
        }
    }
    public class node
    {
        public string name;
        public int age;
        public node next;

        public node()
        {
            next = null;
        }
    }
    public class singlrLinkedList
    {
        public node header;
        public node cur;
        public node newnode;
        public singlrLinkedList()
        {
            header = null;
            cur = null;
        }
        public void addLinkedList(string name ,int age)
        {
            newnode = new LinkedList.node();
            newnode.name = name;
            newnode.age = age;
           
            if (header == null)
            {
                header = newnode;
                cur = header;
            }
            else
            {
                cur.next = newnode;
                cur = cur.next;
            }
        }
        public void deletepos(int i)
        {
            node delx;
            cur = header;
            if(i==1)
            {
                header = cur.next;
                cur = header;
            }
            else
            {
                for(int j=1;j<i-1;j++)
                {
                    cur = cur.next;
                }
                delx = cur.next;
                if(delx != null)
                {
                    cur.next = delx.next;
                }
            }
        }
        public string showall()
        {
            int i = 1;
            string str = "";
            cur = header;
            while(cur !=null)
            {
                str = str + i.ToString() + " : " + cur.name + "," + cur.age.ToString()+Environment.NewLine;
                i++;
                cur = cur.next;
            }         
            return str;
        }
    }
}

วันอาทิตย์ที่ 29 ตุลาคม พ.ศ. 2560

C++ Stack

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <thread>
typedef struct node
{
int data;
struct node* link;
}STACK_NODE;

typedef struct
{
int count;
STACK_NODE *top;

}STACK;

void insertData(STACK* pStack);
void print(STACK* pStack);
bool push(STACK* pList, int dataIn);
bool pop(STACK* pList, int *dataOut);


int main()
{
STACK *pStack;
pStack = new STACK;
printf("=============STACK=============\n\n");
pStack->top = NULL;
pStack->count = 0;
insertData(pStack);
print(pStack);

return 0;
}

void insertData(STACK* pStack)
{
int numIn;
bool success;
srand(time(NULL));
printf("Create Number : ");
for (int nodeCount = 0; nodeCount < 10; nodeCount++)
{
numIn = rand() % 999;
printf("%4d", numIn);
success = push(pStack, numIn);
if (!success)
{
printf("Error out of memory\n");
exit(101);
}
}
printf("\n\n");
return;
}
void print(STACK* pStack)
{
int printData;
printf("Stack contained : \n");
printf("\t\t +--------+\n");
while (!pop(pStack, &printData))
{
printf("\t\t |  %4d  |\n", printData);
}
printf("\t\t +--------+\n");
return;
}
bool push(STACK* pList, int dataIn)
{
STACK_NODE* pNew;
bool success;
pNew = (STACK_NODE*)malloc(sizeof(STACK_NODE));
if (!pNew)
{
success = false;
}
else
{
pNew->data = dataIn;
pNew->link = pList->top;
pList->top = pNew;
pList->count++;
success = true;
}
return success;
}
bool pop(STACK* pList, int *dataOut)
{
STACK_NODE* pDlt;
bool success;

if (pList->top)
{
success = false;
*dataOut = pList->top->data;
pDlt = pList->top;
pList->top = (pList->top)->link;
pList->count--;
free(pDlt);
}
else
{
success = true;
}
return success;
}


วันเสาร์ที่ 14 ตุลาคม พ.ศ. 2560

C++ partial Sum


#include <vector>
#include <iostream>
using namespace std;

double sum(int n)
{
double partialSum = 0.0;
for (int k = 2; k <= n; k++)
{
partialSum += sqrt(k / (k + 1.)) -sqrt((k - 1.) / k);
}
return partialSum;
}
int main(int argc,char*argv)
{
cout << sum(1000) << endl;
return 0;
}





วันเสาร์ที่ 7 ตุลาคม พ.ศ. 2560

C++ opencv edit pixel

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

#include<iostream>
#include<conio.h>         

using namespace std;
using namespace cv;

void procFrame(cv::Mat &frame);
int main(int argc,char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Could not open camera" << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

namedWindow("Exam", WINDOW_AUTOSIZE);
while (true)
{
Mat frame;
if (cap.read(frame))
{
procFrame(frame);
imshow("Exam", frame);
}
if (waitKey(30) == 27)
{
break;
}
}
return 0;
}

void procFrame(cv::Mat & frame)
{
static float alpha = 1.0f;
for (unsigned int z = 0; z<frame.rows; z++)
{
for (unsigned int i = 0; i < frame.cols; i++)
{
Vec3b &cur_pixel = frame.at<Vec3b>(z, i);
cur_pixel[0] *= (-alpha);
cur_pixel[1] *= (-alpha);
cur_pixel[2] *= alpha;
}
}
alpha += 0.1f;
}


C++ opencv drawing Box

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

#include<iostream>
#include<conio.h>       

using namespace std;
using namespace cv;

void my_mouse_calback(int event, int x, int y, int flags, void*data);

Rect Box;
Mat drawing;

bool drawing_box = false;

void draw_box(Mat& img, Rect box)
{
rectangle(img, box.tl(), box.br(), Scalar(255, 0, 0));
}

int main(int argc,char** argv)
{
int Bright = 1, Cont = 47;
Mat drawing, drawing2;
drawing = imread("D:\\3.jpg"); drawing.copyTo(drawing2);
Box = Rect(-1, -1, 0, 0);

namedWindow("Exam", CV_WINDOW_AUTOSIZE);


setMouseCallback("Exam", my_mouse_calback, (void*)&drawing);
while(1)
{
drawing.copyTo(drawing2);
if (drawing_box)draw_box(drawing2, Box);
imshow("Exam", drawing2);
if (waitKey(30) == 27)
break;
}
return 0;
}

void my_mouse_calback(int event, int x, int y, int flags, void*data)
{
Mat& drawing = *(Mat*)data;
int w, h, r;
if (event == EVENT_MOUSEMOVE)
{
if (drawing_box)
{
Box.width = x - Box.x;
Box.height = y - Box.y;
}
}

if (event == EVENT_LBUTTONDOWN)
{
drawing_box = true;
Box = Rect(x, y, 0, 0);
}
if (event == EVENT_LBUTTONUP)
{
drawing_box = false;

if (Box.width < 0)
{
Box.x += Box.width;
Box.width *= -1;
}
if (Box.height < 0)
{
Box.x = Box.height;
Box.width *= -1;
}
draw_box(drawing, Box);
}
}


วันอาทิตย์ที่ 11 มิถุนายน พ.ศ. 2560

C++ queue

#include <iostream>
using namespace std;
#define N 11
int queuedata[N];
int front = 0;
int rear = 0;

int add(int data)
{
if (rear < N - 1)
{
rear++;
queuedata[rear] = data;
if (!front)
return -1;
return 1;
}
return -1;
}
int get()
{
int temp;
if (front)
{
temp = queuedata[front];
if (front > rear)
{
front = 0;
rear = 0;
return -1;
}
else
front++;
}
else
return -1;

return temp;
}
void show()
{
for (int i = front+1; i < rear + 1; i++)
{
cout << queuedata[i] << " ";
}
cout << endl;
}

int main(int argc, char** argv)
{
add(5);
add(8);
get();
show();
return 0;
}

C++ stack

#include <iostream>
using namespace std;
#define N 10
int stackdata[N];
int top = -1;

int push(int n)
{
if (top < N - 1)
{
top++;
stackdata[top] = n;
return 1;
}
return -1;
}
int pop()
{
int r;
if (top > -1)
{
r = stackdata[top];
stackdata[top] = 0;
top--;
return r;
}
return -1;
}
int main(int argc, char** argv)
{
push(1);
push(3);
pop();
push(9);
push(9);
push(3);

for (int i = 0; i <= top; i++)
{
cout << stackdata[i] << " ";
}
cout << endl;
return 0;
}

วันเสาร์ที่ 10 มิถุนายน พ.ศ. 2560

C++ การเรียงกลับหลังใน Array

#include <iostream>
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int i;

for (i = 0; i < N / 2; i++)
{
int temp;
temp = data[i];
data[i] = data[N - 1 - i];
data[N - 1 - i] = temp;
}

for (int i = 0; i < N; i++)
cout << data[i] << " ";

cout << endl;
return 0;
}

C++ การลบข้อมูลใน Array

#include <iostream>
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int position = 3;
int i;

for (i = position; i < N; i++)
data[i - 1] = data[i];
data[i - 1] = -1;

for (int i = 0; i < N; i++)
cout << data[i] << " ";

cout << endl;
return 0;
}

C++ การแทรกข้อมูลใน Array

#include <iostream>
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int newdata = 6;
int position = 3;
int i;
for (i = N - 1; i >= position; i--)
data[i] = data[i - 1];
data[i] = newdata;

for (int i = 0; i < N; i++)
{
cout << data[i] << " ";
}
cout << endl;
return 0;
}

C++ ค้นหาข้อมูลใน Array

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
int data[5] = { 1,2,3,4,5 };
int count = 0;
bool h = false;
int search = 5;
while (count < 5)
{
if (data[count] == search)
{
cout << "Have data at position " << count+1 << endl;
h = true;
break;
}
count++;
}
if (h == false)
cout << "Have not data in array" << endl;
return 0;
}

วันพฤหัสบดีที่ 23 มีนาคม พ.ศ. 2560

C++ opencv Thresholds

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// Global variables

int threshold_value = 0;
int threshold_type = 3;;
int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;

Mat frame, src_gray, dst;
char* window_name = "Threshold Demo";

char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
char* trackbar_value = "Value";

/// Function headers
void Threshold_Demo(int, void*);

int main(int argc, char** argv)
{
VideoCapture cap(0);
while (1)
{

cap >> frame;
/// Convert the image to Gray
cvtColor(frame, src_gray, CV_BGR2GRAY);

/// Create a window to display results
namedWindow(window_name, CV_WINDOW_AUTOSIZE);

/// Create Trackbar to choose type of Threshold
createTrackbar(trackbar_type,
window_name, &threshold_type,
max_type, Threshold_Demo);

createTrackbar(trackbar_value,
window_name, &threshold_value,
max_value, Threshold_Demo);

/// Call the function to initialize
Threshold_Demo(0, 0);
if (waitKey(30) == 27)
break;

}
return 0;
}

void Threshold_Demo(int, void*)
{
/* 0: Binary
1: Binary Inverted
2: Threshold Truncated
3: Threshold to Zero
4: Threshold to Zero Inverted
*/
threshold(src_gray, dst, threshold_value, max_BINARY_value, threshold_type);
imshow(window_name, dst);
}

C++ opencv Threshold

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;

/// Function header
void thresh_callback(int, void*);

/** @function main */
int main(int argc, char** argv)
{
VideoCapture cap(0);
while (1)
{
cap >> src;
/// Convert image to gray and blur it
cvtColor(src, src_gray, CV_BGR2GRAY);
blur(src_gray, src_gray, Size(3, 3));

/// Create Window
char* source_window = "Source";
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
imshow(source_window, src);

createTrackbar(" Canny thresh:", "Source", &thresh, max_thresh, thresh_callback);
thresh_callback(0, 0);
if (waitKey(30) == 27)
break;
}
return(0);
}

/** @function thresh_callback */
void thresh_callback(int, void*)
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

/// Detect edges using canny
Canny(src_gray, canny_output, thresh, thresh * 2, 3);
/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
for (int i = 0; i< contours.size(); i++)
{
drawContours(drawing, contours, i, Scalar(255,255,255), 1, 8, hierarchy, 0, Point());
}

/// Show in a window
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);
}

C++ opencv Template Matching

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/// Global Variables
Mat img; Mat templ; Mat result;
char* image_window = "Source Image";
char* result_window = "Result window";

int match_method;
int max_Trackbar = 5;

/// Function Headers
void MatchingMethod(int, void*);

/** @function main */
int main(int argc, char** argv)
{
VideoCapture cap(0);

while (1)
{
cap >> img;

templ = imread("D:\\121.jpg", 1);
namedWindow("template", CV_WINDOW_AUTOSIZE);
imshow("template", templ);
/// Create windows
namedWindow(image_window, CV_WINDOW_AUTOSIZE);
namedWindow(result_window, CV_WINDOW_AUTOSIZE);

/// Create Trackbar
char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
createTrackbar(trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod);

MatchingMethod(0, 0);
if (waitKey(30) == 27)
break;
}
return 0;
}

/**
* @function MatchingMethod
* @brief Trackbar callback
*/
void MatchingMethod(int, void*)
{
/// Source image to display
Mat img_display;
img.copyTo(img_display);

/// Create the result matrix
int result_cols = img.cols * 2 - templ.cols + 1;
int result_rows = img.rows * 2 - templ.rows + 1;

result.create(result_rows, result_cols, CV_32FC1);

/// Do the Matching and Normalize
matchTemplate(img, templ, result, match_method);
normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());


/// Localizing the best match with minMaxLoc
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;

minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());

/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if (match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED)
{
matchLoc = minLoc;
}
else
{
matchLoc = maxLoc;
}

/// Show me what you got
rectangle(img_display, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar::all(0), 2, 8, 0);
rectangle(result, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar::all(0), 2, 8, 0);

imshow(image_window, img_display);
imshow(result_window, result);

return;
}

C++ opencv Sobel

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/** @function main */
int main(int argc, char** argv)
{
Mat frame, src_gray;
Mat grad;
char* window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;

int c;

VideoCapture cap(0);
while (1)
{
cap >> frame;
GaussianBlur(frame, frame, Size(3, 3), 0, 0, BORDER_DEFAULT);
/// Convert it to gray
cvtColor(frame, src_gray, CV_BGR2GRAY);

/// Create window
namedWindow(window_name, CV_WINDOW_AUTOSIZE);

/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;

/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);

/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_y, abs_grad_y);

/// Total Gradient (approximate)
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 50, grad);

imshow(window_name, grad);
if (waitKey(30) == 27)
break;
}
return 0;
}

C++ opencv Smooth Image

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

/// Global Variables
int DELAY_CAPTION = 1500;
int DELAY_BLUR = 100;
int MAX_KERNEL_LENGTH = 31;

Mat frame;
Mat dst;

int main(int argc, char** argv)
{
VideoCapture cap(0);
while (1)
{
cap >> frame;
dst = frame.clone();
namedWindow("Original image", CV_WINDOW_AUTOSIZE);
imshow("Original image", frame);

blur(frame, dst, Size(25, 25), Point(-1, -1));

namedWindow("Homogeneous Blur", CV_WINDOW_AUTOSIZE);
imshow("Homogeneous Blur", dst);

/// Applying Gaussian Blur
GaussianBlur(frame, dst, Size(25, 25), 0, 0);

namedWindow("Gaussian Blur", CV_WINDOW_AUTOSIZE);
imshow("Gaussian Blur", dst);

/// Applying Median blur

medianBlur(frame, dst, 25);

namedWindow("Median Blur", CV_WINDOW_AUTOSIZE);
imshow("Median Blur", dst);

/// Applying Bilateral Filter

bilateralFilter(frame, dst, 25, 25 * 2, 25 / 2);

namedWindow("Bilateral Blur", CV_WINDOW_AUTOSIZE);
imshow("Bilateral Blur", dst);

if (waitKey(30) == 27)
break;
}
return 0;
}

C++ opencv Remapping

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;

/// Global variables
Mat frame, dst;
Mat map_x, map_y;
char* remap_window = "Remap demo";
int ind = 0;

/// Function Headers
void update_map(void);

/**
* @function main
*/
int main(int argc, char** argv)
{

VideoCapture cap(0);
while (1)
{

cap >> frame;
namedWindow("Original", CV_WINDOW_AUTOSIZE);
imshow("Original", frame);

/// Create dst, map_x and map_y with the same size as src:
dst.create(frame.size(), frame.type());
map_x.create(frame.size(), CV_32FC1);
map_y.create(frame.size(), CV_32FC1);

// LLop for reducing half the size and centering
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

if (i > frame.cols*0.25 && i < frame.cols*0.75 && j > frame.rows*0.25 && j < frame.rows*0.75)
{
map_x.at<float>(j, i) = 2 * (i - frame.cols*0.25) + 0.5;
map_y.at<float>(j, i) = 2 * (j - frame.rows*0.25) + 0.5;
}
else
{
map_x.at<float>(j, i) = 0;
map_y.at<float>(j, i) = 0;
}
}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("reducing half the size and centering", CV_WINDOW_AUTOSIZE);
imshow("reducing half the size and centering", dst);

//Loop for upside down
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = i;
map_y.at<float>(j, i) = frame.rows - j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("upside down", CV_WINDOW_AUTOSIZE);
imshow("upside down", dst);

//Loop for Reflecting x direction
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = frame.cols - i;
map_y.at<float>(j, i) = j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("Reflecting x direction", CV_WINDOW_AUTOSIZE);
imshow("Reflecting x direction", dst);

//Loop for Reflecting both directions
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = frame.cols - i;
map_y.at<float>(j, i) = frame.rows - j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("Reflecting both directions:", CV_WINDOW_AUTOSIZE);
imshow("Reflecting both directions:", dst);
if (waitKey(30) == 27)
break;
}
return 0;
}

C++ opencv Remapping

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;

/// Global variables
Mat frame, dst;
Mat map_x, map_y;
char* remap_window = "Remap demo";
int ind = 0;

/// Function Headers
void update_map(void);

/**
* @function main
*/
int main(int argc, char** argv)
{

VideoCapture cap(0);
while (1)
{

cap >> frame;
namedWindow("Original", CV_WINDOW_AUTOSIZE);
imshow("Original", frame);

/// Create dst, map_x and map_y with the same size as src:
dst.create(frame.size(), frame.type());
map_x.create(frame.size(), CV_32FC1);
map_y.create(frame.size(), CV_32FC1);

// LLop for reducing half the size and centering
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

if (i > frame.cols*0.25 && i < frame.cols*0.75 && j > frame.rows*0.25 && j < frame.rows*0.75)
{
map_x.at<float>(j, i) = 2 * (i - frame.cols*0.25) + 0.5;
map_y.at<float>(j, i) = 2 * (j - frame.rows*0.25) + 0.5;
}
else
{
map_x.at<float>(j, i) = 0;
map_y.at<float>(j, i) = 0;
}
}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("reducing half the size and centering", CV_WINDOW_AUTOSIZE);
imshow("reducing half the size and centering", dst);

//Loop for upside down
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = i;
map_y.at<float>(j, i) = frame.rows - j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("upside down", CV_WINDOW_AUTOSIZE);
imshow("upside down", dst);

//Loop for Reflecting x direction
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = frame.cols - i;
map_y.at<float>(j, i) = j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("Reflecting x direction", CV_WINDOW_AUTOSIZE);
imshow("Reflecting x direction", dst);

//Loop for Reflecting both directions
for (int j = 0; j < frame.rows; j++)
{
for (int i = 0; i < frame.cols; i++)
{

map_x.at<float>(j, i) = frame.cols - i;
map_y.at<float>(j, i) = frame.rows - j;

}
}
remap(frame, dst, map_x, map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
namedWindow("Reflecting both directions:", CV_WINDOW_AUTOSIZE);
imshow("Reflecting both directions:", dst);
if (waitKey(30) == 27)
break;
}
return 0;
}

C++ opencv Pyramid

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <stdlib.h>
#include <iostream>

using namespace cv;
using namespace std;
/// Global variables
Mat frame, dst;

int main(int argc, char** argv)
{
VideoCapture cap(0);

while (1)
{
cap >> frame;

dst = frame.clone();
namedWindow("Original image", CV_WINDOW_AUTOSIZE);
imshow("Original image", frame);


/// Zoom Up
pyrUp(frame, dst, Size(frame.cols * 2, frame.rows * 2));
cout << "** Zoom In: Image x 2"<<endl;
namedWindow("Pyramids Up", CV_WINDOW_AUTOSIZE);
imshow("Pyramids Up", dst);

/// Zoom Down
pyrDown(frame, dst, Size(frame.cols / 2, frame.rows / 2));
cout << "** Zoom Out: Image / 2" << endl;

namedWindow("Pyramids Down", CV_WINDOW_AUTOSIZE);
imshow("Pyramids Down", dst);

if (waitKey(30) == 27)
break;
}
return 0;
}

C++ opencv Morphological

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

/// Global variables
Mat frame, dst;

int morph_elem = 0;
int morph_size = 0;
int morph_operator = 0;
int const max_operator = 4;
int const max_elem = 2;
int const max_kernel_size = 21;

/** Function Headers */
void Morphology_Operations(int, void*);

/** @function main */
int main(int argc, char** argv)
{
VideoCapture cap(0);

while (1)
{
cap >> frame;
/// Create window
namedWindow("Morphological", CV_WINDOW_AUTOSIZE);

/// Create Trackbar to select Morphology operation
createTrackbar("Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat", "Morphological", &morph_operator, max_operator, Morphology_Operations);

/// Create Trackbar to select kernel type
createTrackbar("Element:\n 0: Rect - 1: Cross - 2: Ellipse", "Morphological",
&morph_elem, max_elem,
Morphology_Operations);

/// Create Trackbar to choose kernel size
createTrackbar("Kernel size:\n 2n +1", "Morphological",
&morph_size, max_kernel_size,
Morphology_Operations);

/// Default start
Morphology_Operations(0, 0);
if (waitKey(30) == 27)
break;

}
return 0;
}

//function Morphology_Operations

void Morphology_Operations(int, void*)
{
// Since MORPH_X : 2,3,4,5 and 6
int operation = morph_operator + 2;

Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));

/// Apply the specified morphology operation
morphologyEx(frame, dst, operation, element);
imshow("Morphological", dst);
}

C++ opencv Moment

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

/// Function header
void thresh_callback(int, void*);

/** @function main */
int main(int argc, char** argv)
{
VideoCapture cap(0);

while (1)
{
cap >> src;
/// Convert image to gray and blur it
cvtColor(src, src_gray, CV_BGR2GRAY);
blur(src_gray, src_gray, Size(3, 3));

/// Create Window
char* source_window = "Source";
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
imshow(source_window, src);

createTrackbar(" Canny thresh:", "Source", &thresh, max_thresh, thresh_callback);
thresh_callback(0, 0);
if (waitKey(30) == 27)
break;
}
return(0);
}

/** @function thresh_callback */
void thresh_callback(int, void*)
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

/// Detect edges using canny
Canny(src_gray, canny_output, thresh, thresh * 2, 3);
/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

/// Get the moments
vector<Moments> mu(contours.size());
for (int i = 0; i < contours.size(); i++)
{
mu[i] = moments(contours[i], false);
}

///  Get the mass centers:
vector<Point2f> mc(contours.size());
for (int i = 0; i < contours.size(); i++)
{
mc[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
}
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
for (int i = 0; i< contours.size(); i++)
{
drawContours(drawing, contours, i, Scalar(0,0,255), 1, 8, hierarchy, 0, Point());
circle(drawing, mc[i], 4, Scalar(0,255,125), -1, 8, 0);
}

/// Show in a window
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);

/// Calculate the area with the moments 00 and compare with the result of the OpenCV function
printf("\t Info: Area and Contour Length \n");
for (int i = 0; i< contours.size(); i++)
{
printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", i, mu[i].m00, contourArea(contours[i]), arcLength(contours[i], true));
drawContours(drawing, contours, i, Scalar(0,255,0), 1, 8, hierarchy, 0, Point());
circle(drawing, mc[i], 4, Scalar(255,0,0), -1, 8, 0);
}
}

C++ opencv Linear Filter

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
/// Declare variables
Mat frame, dst, kernel;
int kernel_size;

VideoCapture cap(0);
while (1)
{
cap >> frame;
dst = frame.clone();

namedWindow("Original number", CV_WINDOW_AUTOSIZE);
imshow("Original number", frame);

/// Update kernel size for a normalized box filter
kernel_size = 15;
//3 + 2 * (20 % 5);
kernel = Mat::ones(kernel_size, kernel_size, CV_32F) / (float)(kernel_size*kernel_size);

/// Apply filter
filter2D(frame, dst, -1, kernel, Point(-1, -1), 0, BORDER_DEFAULT);

namedWindow("filter2D Demo", CV_WINDOW_AUTOSIZE);
imshow("filter2D Demo", dst);
if (waitKey(30) == 27)
break;
}
return 0;
}

C++ opencv Laplace

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/** @function main */
int main(int argc, char** argv)
{
Mat frame, src_gray, dst;
int kernel_size = 5;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
char* window_name = "Laplace Demo";

int c;

VideoCapture cap(0);
while (1)
{
cap >> frame;
/// Remove noise by blurring with a Gaussian filter
GaussianBlur(frame, frame, Size(3, 3), 0, 0, BORDER_DEFAULT);

/// Convert the image to grayscale
cvtColor(frame, src_gray, CV_BGR2GRAY);

/// Create window
namedWindow(window_name, CV_WINDOW_AUTOSIZE);

/// Apply Laplace function
Mat abs_dst;

Laplacian(src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT);
convertScaleAbs(dst, abs_dst);

/// Show what you got
imshow(window_name, abs_dst);
if (waitKey(30) == 27)
break;
}

return 0;
}