Here i put codes that i wrote in the past as practice either for online courses or for platforms like hackerrank or something
Here are some notable learnings
- getattr(arr,command)(*n) --> from practice number 6.
the *n operator is iterating operator.
getattr will retrieve the type of the input and allow you to work on it like a variable
eg here, arr is a list, and command can be "append" or "pop".
getattr gets the variable type of arr, which is list.
- img = cv2.imread(imgfile)
- img.shape -- this is actually a numpy command, img.shape[0] --> height, img.shape[1] --> width basically img_h, img_w = img.shape
- img.dtype -- returns type of varialbe
- cv2.imshow('name of window', img_variable)
- cv2.videowriter
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # deciding codec and video format
fps = 30
cv2.VideoWriter(output_path_name, fourcc, fps, (sampleimage_w, sampleimage_h ))
pls rmb to do "video.release()" \
- cv2.waitKey(0) -- imshow() should be followed by function waitKey(), which specifies how long the image should be specified in milliseconds
- cv2.destroyAllWindows() -- you need to do this else cv2.imshow won't work again in the same run
- For a colour image, imread() gives a 3D numpy array in BGR format
- img[:,:,0] --> Blue channel
- img[:,:,1] --> Green channel
- img[:,:,2] --> Red channel
- cv2.cvtcolor(img, cv2.COLOR_BGR2GRAY) -- many conversion codes, see their docs
- cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) -- convert grayscale back to BGR, but actually just copies the vals to 3 channels, you wont get back your colour information
- cv2.merge(imgc1, imgc2, imgc3) -- stack the channels together
- cv2.copyMakeBorder(img, top, btm, left, right, borderType) -- cv2.BORDER_REPLICATE, cv2.BORDER_REFLECT, cv2.BORDER_WRAP, cv2.BORDER_CONSTANT
- cv2.line
- cv2.circle
- cv2.rectangle
- cv2.ellipse
- cv2.putText
- cv2.resize -- common methods cv2.INTER_AREA, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_NEAREST
- cv2.affine(img, transform_matrix, (output height,output width))
- cv2.getRotationMatrix2D
- cv2.equalizeHist -- histogram equalisation, gives less confusing edges when doing edge detection
- cv2.createCLAHE() -- CLAHE Contrast limited adapative histogram equalisation, visually clearer clahe.apply(img)
- cv2.threshold -- remember Otsu thresholding too
- cv2.filter(img, intensity, kernel) -- for filtering using self defined kernel... convolutions
- cv2.blur(img, (kernelsize,kernelsize)) -- mean filtering
- cv2.GaussianBlur(img, (kernelsize,kernelsize), val) -- val is sigma value, set to 0 to let opencv decide
- cv2.medianBlur
- To sharpen image...where nuc is image file. Values are not fixed, just an example
krn = np.ones((11,11),np.float32)/121 mnuc = cv2.filter2D(nuc,-1,krn) sharpen = (np.float32(nuc)*2 - np.float32(mnuc))/255 Note: sharpening introduces noise into image
- canny_edges = cv2.Canny(img, minval, maxval, 3) -- last argument is kernel size, usually defaults to 3, can use 5 or 7 too
contours = cv2.findContours -- find contours cv2.drawContours -- draw contours
- Haar Cascade classifier/Viola Jones algorithm
fce = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #or whatever xml file face= fce.detectMultiScale(img, scaleFactor, minNeighbours) sml = cv2.CascadeClassifier('haarcascade_smile.xml') # or whatever your xml file name is smle= sml.detectMultiScale(fcb, scaleFactor=1.3, minNeighbors=10) # or whatever numbers
- CaffeModel/GoogLeNet
prototxt = 'bvlc_googlenet.prototxt' caffemodel= 'bvlc_googlenet.caffemodel' net = cv2.dnn.readNetFromCaffe(prototxt,caffemodel) blob = cv2.dnn.blobFromImage(image=img, scalefactor, outputsize, mean, swapRB, crop) net.setInput(blob) preds = net.forward()
-
cv2.dilate -- grow everything
-
cv2.erode -- shave off from everything
-
yolov3 in opencv
yoloconfig = 'yolov3.cfg' yoloweights= 'yolov3.weights' net = cv2.dnn.readNet(yoloweights,yoloconfig) blob = cv2.dnn.blobFromImage(image=img, scalefactor, outputsize, mean, swapRB, crop) net.setInput(blob) outLyrs = getOutputLayers(net) #get output layers preds = net.forward(outLyrs) #Run the actual object detection. cv2.dnn.NMSBoxes(bboxes=boxes, scores=confidences, score_threshold=scoreThres, nms_threshold=nmsThres) #do non max suppression
- cv2.morphologyEx
- ms.inverse_gaussian_gradient(img, alpha, sigma)
- ms.circle_level_set(img.shape, (centerx, centery), radius)
- ms.visual2d -- visualisation callback
- ms.morphological_geodesic_active_contour(inversegauss, iterations, init, smoothing, threshold, balloon, iter_callback=back)
- plt.axis('on') or plt.axis('off')
- plt.imshow
- plt.show
- plt.hist.img.ravel()
- np_array = numpy.array(arr, float) --> converts a list called arr into a numpy array, you can specify the value type. Eg float int double
- np.transpose
- np.matmul --> to do matrix multiplication on 2 matrices (max is 2 for this function)
- np.array --> to convert a list into an array
- np.linalg.inv --> to calculate the inverse of a matrix
- @ operator --> usin the @ operator lets you do multiple matrix multiplcations in 1 line in that specified order
eg Pmatrix = np.linalg.inv( np.matmul(np.matmul(H_trans, R_inv), Hmatrix))
Pmatrix = np.linalg.inv( H_trans @ R_inv @ Hmatrix)
These 2 lines are equivalent in their operation
- a = int(input()) ---> this reads the input and convert into a int. Else it is a string.
- rooms = input().split() ----> splits a long input in a list. For eg, 1 4 5 a b c becomes '1', '4', '5', 'a', 'b', 'c'
- string.isalnum()
- str.isalpha()
- str.islower()
- str.isupper()
- str.ljust(width, padding_chars)
- str.rjust(width, padding_chars)
- str.center(width, padding_chars)
- str.join() --> see #https://www.w3schools.com/python/ref_string_join.asp
normal = str(i)
octal = format(i,'o')
hexa = format(i, 'X')
binary = format(i, 'b')
- dictonary_name.items() gives you key (key, value) tuples:
import collections
from collections import Counter #https://www.geeksforgeeks.org/python-collections-module/
print(Counter(['B','B','A','B','C','A','B','B','A','C'])) #Counter gives you a dictionary
{'B': 5, 'A': 3, 'C': 2}
sets only retain unique values, list retains whatever you put in
scores_set = set(scores) #set only retains unique values, you can convert a list into a set like that
itertools.product()