Saturday, April 11, 2020

Computer Vision learning




Computer Vision is the field advanced from image processing not just image capturing, formation, or restoration but also to extract the information from it and often its extrapolation also. These information are often related to objects present in the frame. some of the application are :

1. Object detection
2. Object tracking
3. Image classification/Segmentation
4. Motion oriented transformations
5. Face , Iris, Smile detection
6. Depth and 3D construction
7. Optical Character recognition (OCR)

 and several others things and fields like computational photography, Automotives - autonomous vehicle controls and safety, Medical Imaging etc.

Resources to learn it : Book reference : Computer Vision: Algorithms and Applications by Richard Szeliski   , Best place to learn it.

All these above mentioned features are mainly based on relationship between image, geometry and photometry. They uses fundamental 2d and 3d primitives
into their algorithms.

OpenCV is open source computer vision library, it has very large number of algorithms implemented and utilities for several image operations activity.

Image in OpenCV : Image gets read as matrix and mat (image object) classes has methods required for image operations like read, write, display, access image properties like color, channel, shape, size, pixels manipulation. Mat has matrix header and pointer to the matrix containing pixels. the matrix header gets used in reference
counting when there is shared address matrix.

Image Read, Modify and Display :

Mat cv::imread(const string& filename, int flag = IMREAD_COLOR)

IMREAD_GRAYSCALE = 0
IMREAD_COLOR = 1
IMREAD_UNCHANGED = -1  , load as it is, including alpha

using namespace cv;
Mat img = imread(imagepath, 0);

where img is image object allocated automatically , imread loads the image into it. imread reads images in blue green red BGR format.

Image data type:



Modify:

img.at<uchar>(1,1)=128;
img(Range(0,1), Range(0,3)).setTo(128);


Display:

for float datatype - value range is from 0 to 1, for int its 0 to 255

Matplotlib and imshow:

1. plt::figure_size(600, 400);
    plt::imshow(img);
    auto imgPlot = displayImg(img);
    imgPlot

2.  imshow("image", img);
     waitKey(0);
     destroyAllWindows();

Others:
a. named window

void cv::namedWindow (const  string&  windowName, int flag = WINDOW_AUTOSIZE)

b.  waitKey

int cv::waitKey(int delay = 0)  , 0 waits for key press

c. destroyWindow

void cv::destroyWindow(const string&  windowName)

d. destroyAllWindow

void cv::destroyAllWindow()


Saving Image:

bool cv::imwrite(const string& filename, InputArray img, const vector<int>&
      params = std::vector<int>())

imwrite("../testImg.jpg", img);

Color space:

OpenCV reads a given image in the BGR format by default. So, you’ll need to change the color space of your image from BGR to RGB when reading images using OpenCV

cv::cvtColor(img,cv.COLOR_BGR2GRAY)


Color Images

img.size() -> y x x
img.Channels() -> 3

Channel Operations : Split and Merge

Mat Chnls[3]

split(img, Chnls);

can be seen using displayImage(Chnls[i]) or imshow(Chnls[i])


Accessing color pixels

img.at<Vec3b>(0,0); --> gets 3 bytes in vector format

[1,1,1] --> intensity values

Modifying ->

img.at<Vec3b>(0,0) = Vec3b(0,255,255); B, G, R --> G and R --> yellow

Region :

img(Range(0,2), Range(0,3).setTo(Scalar(255,0,0));


Alpha channel :

Mat img = imread(img, -1);

Mat imgChanl[4];

split(img, imgChanl); --> imgChanl[i] -> points to each channel , B, G, R , A intensity value ranging from 0 to 255


========================================================

Create, Crop Images:

Mat img = Mat(400, 600, CV_8UC4, Scalar(128,128,128,0));

Mat img1 = img.clone();

Cropping
Mat  crop = img(Range(100, 200), Range(100, 400));
y --> 100 to 200
x --> 200 to 400

Resizing an Image:

void cv::resize( InputArray src,  --> input image
      OutputArray dst,  --> output image
      Size dstSize,   --> output size
      double fx = 0,
      double fy = 0,
      int interpolation = INTER_LINEAR)  --> bilinear , better quality than nearest neighbor

fx, fy sampling factor in horizontal and vertical axis
interpolation are usually nearest neighbor, bilinear, bicubic 

Image Masking:

used to segment out area of interest and not.

Mat mask = Mat::zeroes(image.size(), image.type());

mask(Range(40,100), Range(120, 180)).setTo(255);

inRange function : 

void cv::inRange(InputArray src,
     InputArray lower,
     InputArray upper,
     OutputArray dst)

Mat mask;
inRange(image, Scalar(0,0,150), Scalar(100,100,255), mask);




Wednesday, November 13, 2019

Digital Root using Recursion


You are given a number n. You need to find the digital root of n.
DigitalRoot of a number is the recursive sum of its digits until we get a single digit number.
Eg.DigitalRoot(191)=1+9+1=>11=>1+1=>2

int digitalRoot(int n)
{
    int sum = 0;
    if (n < 10)
        return n;
     
    sum = digitalRoot(n / 10) + n % 10;
 
    if (sum / 10 != 0)
    {
        return digitalRoot(sum);
    }
    else
    {
        return sum;
    }
}

Wednesday, March 20, 2019

Good Stuff





1. Eat Brain healthy foods:

1. Avocado
2. Blueberry
3. Broccoli
4. Coconut
5. Eggs
6. Green Vegetables
7. Salmon Fish
8. Turmeric
9. Walnut
10. Orange
11. Apple
12. Banana

2. Get Good Sleep

3. Avoid negativity

4.  Be in positive and goal driven group

5.  Always be curious, ask question - A question opens the mind , statement closes it.

6.  As Albert Einstein said , Imagination is more important than knowledge but  right
imagination will come with creativity and creativity can come after learning skills and
lots of practice.   So Acquire the skill to be able to imagine then to explore which would
eventually help acquire knowledge.

7. Win the morning , Win the day



Monday, September 10, 2018

Implement atoi which converts a string to an integer

Implement atoi which converts a string to an integer

Example 1:
Input: "42"
Output: 42
Example 2:
Input: "   -42                    "
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.
class Solution {
public:
    int myAtoi(string str) {

        str.erase(0, str.find_first_not_of(' '));
        str.erase(str.find_last_not_of(' ')+1);

        int len = str.length();
        long long res = 0;
        bool isnegative = str[0] == '-' ? true : false;
        bool ispositive = str[0] == '+' ? true : false;
        int i;
        if (isnegative || ispositive)
            i = 1;
        else
            i = 0;
        long check = isnegative ? 2147483648 : 2147483647;

        for (; i < len; i++)
        {
            if ((str[i] == ' ') || (str[i] - '0' < 0) || (str[i] - '0' > 9))
                break;
           
            if (((res * 10) < check) && (((res * 10 + str[i] - '0') < check)))
            {
                res =  (res * 10 + str[i] - '0');
            }
            else
            {
                res = (true == isnegative) ? INT_MIN : INT_MAX;
                return res;
            }
        }
       
        if (true == isnegative)
        {
            res = -res;
        }
       
        return res;
    }
};

Sunday, September 9, 2018

Best time to buy and sell the stock - Multiple Transaction

Best time to buy and sell the stock : 

Multiple Transaction : 
If you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note:  you must sell the stock before you buy again

Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
             Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int buyIdx     = 0;
        int currProfit = 0;
        int profit     = 0;
        int maxProfit  = 0;
        
        for (int i = 1; i < prices.size(); i++)
        {
            currProfit = prices[i] - prices[buyIdx];
            
            if (currProfit > profit)
            {
                profit = currProfit;
            }
            else 
            {
                buyIdx = i;
                maxProfit += profit;
                profit = 0;
            }
        }
        
        maxProfit += profit;

        return maxProfit;
    }
};

Wednesday, September 5, 2018

Flip an Image / 2 D matrix

Flip an Image / 2 D matrix

Example 1:
Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
Example 2:
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]



class Solution {
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
        for (int row = 0; row < A.size(); row++)
        {
            int numCol = A[row].size();
            int col;
            for (col = 0; col <  (numCol + 1) / 2; col++)
            {
                int temp = A[row][col] ^ 1;
                A[row][col] = A[row][numCol -1 - col] ^ 1;
                A[row][numCol - 1 - col] = temp;     
            }
        }
        return A;
    }
};

Monday, September 3, 2018

Trigonometric Identities and matrix operations


Rotation matrix :

R = [{cos θ , -sin θ},
         {sinθ , cosθ}]

{X1, Y1} = R x {X0, Y0}
       

Pythagoras theorem:

sin^2 θ + cos^2 θ = 1

1 + cot^2 θ = cosec^2 θ = 1/sin^2θ

tan^2 θ + 1 = sec^2 θ = 1/cos^2θ

Odd and even properties

cos(−x) = cos(x),  sin(−x) = − sin(x),  tan(−x) = − tan(x)

Compound Angle Formula:

cos(A + B) = cos A cos B − sin A sin B
cos(A − B) = cos A cos B + sin A sin B
sin(A + B) = sin A cos B + cos A sin B
sin(A − B) = sin A cos B − cos A sin B
tan(A + B) = (tan A + tan B) / (1 − tan A tan B)
tan(A − B) = (tan A − tan B) / (1 + tan A tan B)

cos 2θ = cos^2 θ − sin^2 θ = 2 cos^2 θ − 1 = 1 − 2 sin^2 θ
 sin 2θ = 2 sin θ cos θ
tan 2θ = 2 tan θ / (1 − tan^2 θ)