The document is https://docs.opencv.org/3.0-beta/modules/shape/doc/shape_transformers.html
void estimateTransformation(InputArray transformingShape, InputArray targetShape, std::vector& matches)
So that i run my code
import cv2
import numpy as np
import matplotlib.pyplot as plt
tps = cv2.createThinPlateSplineShapeTransformer()
sshape = np.array ([[67, 90], [206, 90], [67, 228], [206, 227]], np.float32)
tshape = np.array ([[64, 63], [205, 122], [67, 263], [207, 192]], np.float32)
sshape = sshape.reshape (1, -1, 2)
tshape = tshape.reshape (1, -1, 2)
matches = list ()
matches.append (cv2.DMatch (0, 0, 0))
matches.append (cv2.DMatch (1,1,0))
matches.append (cv2.DMatch (2, 2, 0))
matches.append (cv2.DMatch (3, 3, 0))
tps.estimateTransformation (tshape, sshape, matches)
ret, tshape = tps.applyTransformation (sshape)
img = cv2.imread ('tiger.jpg', 1)
out_img = tps.warpImage (img)
plt.imshow(cv2.cvtColor(out_img, cv2.COLOR_BGR2RGB))
plt.show()
cv2.waitKey(0)
cv2.waitKey(0)
The result looks like reverse which i expected
enter image description here
So, i change my code tps.estimateTransformation (tshape, sshape, matches)
And i got expected result.
enter image description here
This is wrong in document or my code is fail.
Related
I used open cv to remove the labels from the map.
But after that, the place of the labels is still clear.
Have a better solution for removing labels?
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
img = cv.imread("map.jpeg", 0)
image_filtered = cv.GaussianBlur(img, (101, 101), 0)
gradient_x = cv.Sobel(image_filtered, cv.CV_16S, 1, 0, 3)
gradient_y = cv.Sobel(image_filtered, cv.CV_16S, 0, 1, 3)
abs_gradient_x = cv.convertScaleAbs(gradient_x)
abs_gradient_y = cv.convertScaleAbs(gradient_y)
sobel_imgg = cv.addWeighted(abs_gradient_x, 0.5, abs_gradient_y, 0.5, 0)
thresholded = cv.threshold(sobel_imgg, 90, 255, cv.THRESH_BINARY_INV)[1]
img_mask = cv.bitwise_and(img, img, mask = thresholded)
kernel = np.ones((11, 11), np.uint8)
dilate_img = cv.dilate(img_mask, kernel)
kernel = np.ones((9, 9), np.uint8)
erode_img = cv.erode(dilate_img, kernel)
img_mask = cv.bitwise_or(erode_img, img)
plt.subplot(121);plt.imshow(img, cmap="gray");plt.title("with label"),plt.xticks([]);plt.yticks([])
plt.subplot(122);plt.imshow(erode_img, cmap="gray");plt.title("no label"),plt.xticks([]);plt.yticks([])
plt.show()
Original Image
Expected Output.
I am using this code for translating a specific part into the same image, but output is not changing,
import numpy as np
import cv2 as cv
img = cv.imread('eye0.jpg', 0)
rows, cols = img.shape
roi = img[200: 300, 360: 450]
M = np.float32([[1, 0, 100], [0, 1, 50]])
dst = cv.warpAffine(roi, M, roi.shape)
cv.imshow('img', img)
cv.imshow('img', dst)
cv.waitKey(0)
cv.destroyAllWindows()
I see no changes from original image. How can I do so? Moreover, as an openCV newbie I would like to know which function should I use/explore here to get my purpose served?
Copy() function can help you instead of warpAffine(). You can check here also:
Here is output and code:
import numpy as np
import cv2 as cv
img = cv.imread('eye.jpg', 1)
#rows, cols = img.shape
roi = img[80: 100, 140: 160]
img2 = img.copy()
img2[95:115, 140:160]=roi
cv.imshow('img', img)
cv.imshow('imaag', img2)
cv.waitKey(0)
cv.destroyAllWindows()
**Image after warp affine tranformation... but for circling the part it seem difficult..
**
import numpy as np
import cv2 as cv
img = cv.imread('eye.jpg')
roi = img[78: 100, 130: 160]
M = np.float32([[1, 0, 6], [0, 1, 4]])
dst = cv.warpAffine(roi, M, (30, 22))
img[80:102, 132:162] = dst
cv.imwrite("newimage.jpg",img)
cv.imshow('img', img)
cv.imshow('img1',dst)
cv.waitKey(0)
cv.destroyAllWindows()
import numpy as np
import cv2
im = cv2.imread("goldstandard.png")
nemo = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
hsv_nemo = cv2.cvtColor(nemo, cv2.COLOR_RGB2HSV)
dictionaryHSV = {
"greenCombo": [[30, 126, 87], [70, 255, 250]],
'red': [[0, 92, 212], [10, 265, 255]],
'blue': [[110, 7, 214], [130, 255, 255]],
'black': [[0, 0, 0], [10, 10, 40]],
'another1': [[20, 245, 151], [40, 255, 231]],
'pink': [[140, 126, 215], [160, 146, 255]]
}
for r1, r2 in dictionaryHSV.values():
lower = np.array(r1)
upper = np.array(r2)
mask = cv2.inRange(hsv_nemo, lower, upper)
# cv2.imshow("masked",mask)
# cv2.waitKey(0)
nm = np.ones((nemo.shape[0], nemo.shape[1], nemo.shape[2]), dtype=np.uint8)
for i in range(nm.shape[0]):
for j in range(nm.shape[1]):
nm[i][j] = (255, 255, 255)
result = cv2.bitwise_and(nm, nm, mask=mask)
cv2.imshow("mappped", result)
cv2.waitKey(0)
i have curve plot images and i want to separate all curves based on color i am getting a problem when i come across black curve i get black curve along with black text in the plot i want to only get the curve not the text. I used color ranges in "H.S.V" color-space to recognize colors. Thanks in advance.
Extract region inside square.
Remove all non black pixels.
Find all contours.
Select a biggest contour - it will be your curve.
Let's say I have an image of a book cover that I want to "flatten". To do so it seems like I would need to perform 2 perspective transforms: one just for the front cover and one just for the back cover:
What would be the most efficient way to do this?
Using a 600x600 pixel image homograpy-test.jpg:
import cv2
import numpy as np
#load image
img = cv2.imread('homography-test.jpg', cv2.IMREAD_COLOR)
#corners of book covers (before)
frontCoverPtsBefore = np.array([[32, 48], [279, 136], [247, 430], [39, 281]], dtype="float32")
backCoverPtsBefore = np.array([[279, 136], [474, 36], [463, 316], [247, 430]], dtype="float32")
#corners of book covers (after)
frontCoverPtsAfter = np.array([[0, 0], [299, 0], [299, 599], [0, 599]], dtype="float32")
backCoverPtsAfter = np.array([[300, 0], [599, 0], [599, 599], [300, 599]], dtype="float32")
#get the transformation matrices for both covers
M_front = cv2.getPerspectiveTransform(frontCoverPtsBefore, frontCoverPtsAfter)
M_back = cv2.getPerspectiveTransform(backCoverPtsBefore, backCoverPtsAfter)
#warpPerspective both images
img_front = cv2.warpPerspective(img, M_front, (600, 600))
img_back = cv2.warpPerspective(img, M_back, (600, 600))
#copy half of the warped back cover into the warped front cover
np.copyto(img_front[:, 300:, :], img_back[:, 300:, :])
#display before and after
cv2.imshow('img', img)
cv2.imshow('img_front', img_front)
cv2.waitKey(0)
cv2.destroyAllWindows()
Before and After:
I am using OpenCV's MSER feature detector to find text regions. With the following Python code, I can detect texts (and some non-texts) and draw polygonal curves around each alphabet. Now, I need to plot these texts (more specifically each alphabet) using matplotlib using different colors. Different colors are important here. I am new to matplotlib and I cannot figure out how to implement that. I seek your guidance. I do not need a full solution, but some hints will be helpful.
import numpy as np
import cv2
import matplotlib.pyplot as plt #plt.plot(x,y) plt.show()
img = cv2.imread('TestText.png')
mser = cv2.MSER_create()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
regions = mser.detectRegions(gray, None)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(vis, hulls, 1, (0, 255, 0))
# cv2.putText(vis, str('change'), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0))
# cv2.fillPoly(vis, hulls, (0, 255, 0))
# cv2.imwrite("test.png", vis)
cv2.imshow('img', vis)
cv2.waitKey(0)
cv2.destroyAllWindows()
May be, you want the result just like Matlab. You should do more steps to do to get the result. Find the coordinates, modify the values with random color.
Here is my Python 3 code for OpenCV 3.3 .
#!/usr/bin/python3
# 2017.10.05 10:52:58 CST
# 2017.10.05 13:27:18 CST
"""
Text detection with MSER, and fill with random colors for each detection.
"""
import numpy as np
import cv2
## Read image and change the color space
imgname = "handicapSign.jpg"
img = cv2.imread(imgname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
## Get mser, and set parameters
mser = cv2.MSER_create()
mser.setMinArea(100)
mser.setMaxArea(800)
## Do mser detection, get the coodinates and bboxes
coordinates, bboxes = mser.detectRegions(gray)
## Filter the coordinates
vis = img.copy()
coords = []
for coord in coordinates:
bbox = cv2.boundingRect(coord)
x,y,w,h = bbox
if w< 10 or h < 10 or w/h > 5 or h/w > 5:
continue
coords.append(coord)
## colors
colors = [[43, 43, 200], [43, 75, 200], [43, 106, 200], [43, 137, 200], [43, 169, 200], [43, 200, 195], [43, 200, 163], [43, 200, 132], [43, 200, 101], [43, 200, 69], [54, 200, 43], [85, 200, 43], [116, 200, 43], [148, 200, 43], [179, 200, 43], [200, 184, 43], [200, 153, 43], [200, 122, 43], [200, 90, 43], [200, 59, 43], [200, 43, 64], [200, 43, 95], [200, 43, 127], [200, 43, 158], [200, 43, 190], [174, 43, 200], [142, 43, 200], [111, 43, 200], [80, 43, 200], [43, 43, 200]]
## Fill with random colors
np.random.seed(0)
canvas1 = img.copy()
canvas2 = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
canvas3 = np.zeros_like(img)
for cnt in coords:
xx = cnt[:,0]
yy = cnt[:,1]
color = colors[np.random.choice(len(colors))]
canvas1[yy, xx] = color
canvas2[yy, xx] = color
canvas3[yy, xx] = color
## Save
cv2.imwrite("result1.png", canvas1)
cv2.imwrite("result2.png", canvas2)
cv2.imwrite("result3.png", canvas3)
The original image (handicapSign.jpg):
The result: