Wednesday, January 6, 2021

How can I remove background from images using OpenCV python ?

import cv2

import numpy as np


#== Parameters =======================================================================

BLUR = 21

CANNY_THRESH_1 = 10

CANNY_THRESH_2 = 200

MASK_DILATE_ITER = 10

MASK_ERODE_ITER = 10

MASK_COLOR = (0.0,0.0,1.0) # In BGR format



#== Processing =======================================================================


#-- Read image -----------------------------------------------------------------------

img = cv2.imread('C:/Temp/person.jpg')

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


#-- Edge detection -------------------------------------------------------------------

edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)

edges = cv2.dilate(edges, None)

edges = cv2.erode(edges, None)


#-- Find contours in edges, sort by area ---------------------------------------------

contour_info = []

_, contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

# Previously, for a previous version of cv2, this line was: 

#  contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

# Thanks to notes from commenters, I've updated the code but left this note

for c in contours:

    contour_info.append((

        c,

        cv2.isContourConvex(c),

        cv2.contourArea(c),

    ))

contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)

max_contour = contour_info[0]


#-- Create empty mask, draw filled polygon on it corresponding to largest contour ----

# Mask is black, polygon is white

mask = np.zeros(edges.shape)

cv2.fillConvexPoly(mask, max_contour[0], (255))


#-- Smooth mask, then blur it --------------------------------------------------------

mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)

mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)

mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)

mask_stack = np.dstack([mask]*3)    # Create 3-channel alpha mask


#-- Blend masked img into MASK_COLOR background --------------------------------------

mask_stack  = mask_stack.astype('float32') / 255.0          # Use float matrices, 

img         = img.astype('float32') / 255.0                 #  for easy blending


masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR) # Blend

masked = (masked * 255).astype('uint8')                     # Convert back to 8-bit 


cv2.imshow('img', masked)                                   # Display

cv2.waitKey()


#cv2.imwrite('C:/Temp/person-masked.jpg', masked)           # Save

how to blur background using opencv python

    import cv2

import numpy as np

img = cv2.imread("C:/my_pics/rahul.png")
blurred_img = cv2.GaussianBlur(img, (21, 21), 0)

mask = np.zeros((512, 512, 3), dtype=np.uint8)
mask = cv2.circle(mask, (258, 258), 100, np.array([255, 255, 255]), -1)

out = np.where(mask==np.array([255, 255, 255]), img, blurred_img)

cv2.imwrite("./out.png", out)

Monday, January 4, 2021

Which datatype should I use in c# while SQL column is money type

 The decimal keyword indicates a 128-bit data type. Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

You can use a decimal as follows:

decimal myMoney = 300.5m;

how to get total number of public properties in a class c#

 class MyClass

{  
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }

    public MyClass()
    {
        int count = this.GetType().GetProperties().Count();
        // or
        count = typeof(MyClass).GetProperties().Count();
    }
}

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...