Feature extraction of image by ORB - python

I want to extract features of text in an image ,
I applied otsu thresholding , followed by erosion ,dilation , found contours and drew them
In the end I used ORB algortithm on preprocessed image
def ORB_descp(ppi):
orb = cv2.ORB_create()
kp = orb.detect(ppi,None)
kp, des = orb.compute(ppi, kp)
return cv2.drawKeypoints(ppi,kp,None,color=(0,255,0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS),kp,des
now i printed des in above code and i got a 2D array where each array within 2D array contains 32 integer something like that
des= [ [180 246 202 86 211 123 167 253 114 159 100 222 64 224 239 159 57 206
130 186 113 247 188 106 56 7 101 45 233 121 238 176] ,................
..,[189 127 34 83 211 191 182 220 55 110 125 255 74 245 239 156 61 250
135 246 254 182 56 62 123 205 37 231 251 247 238 215] ]
What is this array of des,are they features of image ?
If they are how can I use them for text recognition and if not how can I extract feature of text in image for text recognition .
I have to use these features to train SVM , but I am struck at this point
I am not able to extract features.

Related

Numpy Resize Image

I would like to take an image and change the scale of the image, while it is a numpy array.
I would like to do it with native NumPy functions w/o PIL, cv2, SciPy etc
now I have this:
from copy import copy
import numpy as np
from scipy import misc
img = misc.face() # racoon from SciPy(np.ndarray)
img2 = copy(img) # copy of racoon, because misc.face() is Descriptor(?)
img2.shape() # (768, 1024, 3)
Which I need shape = (3072, 4096, 3)
I can do it easy with Pillow
CONVERT_IMAGE = Image.fromarray(img.astype('uint8'), 'RGB')
CONVERT_IMAGE = CONVERT_IMAGE.resize((4096, 3072), Image.NEAREST)
IMAGE_AS_ARRAY = np.asarray(CONVERT_IMAGE)
IMAGE_AS_ARRAY.shape # 3072 4096 3
but I realy need to do this only with NumPy functions w/o other libs
Can you help me ? I'm really weak in NumPy and 3D-arrays
Limited to whole integer upscaling with some scaling factor n and without actual interpolation, you could use np.repeat twice to get the described result:
import numpy as np
# Original image with shape (4, 3, 3)
img = np.random.randint(0, 255, (4, 3, 3), dtype=np.uint8)
# Scaling factor for whole integer upscaling
n = 4
# Actual upscaling (results to some image with shape (16, 12, 3)
img_up = np.repeat(np.repeat(img, n, axis=0), n, axis=1)
# Outputs
print(img[:, :, 1], '\n')
print(img_up[:, :, 1])
Here's some output:
[[148 242 171]
[247 40 152]
[151 131 198]
[ 23 185 144]]
[[148 148 148 148 242 242 242 242 171 171 171 171]
[148 148 148 148 242 242 242 242 171 171 171 171]
[148 148 148 148 242 242 242 242 171 171 171 171]
[148 148 148 148 242 242 242 242 171 171 171 171]
[247 247 247 247 40 40 40 40 152 152 152 152]
[247 247 247 247 40 40 40 40 152 152 152 152]
[247 247 247 247 40 40 40 40 152 152 152 152]
[247 247 247 247 40 40 40 40 152 152 152 152]
[151 151 151 151 131 131 131 131 198 198 198 198]
[151 151 151 151 131 131 131 131 198 198 198 198]
[151 151 151 151 131 131 131 131 198 198 198 198]
[151 151 151 151 131 131 131 131 198 198 198 198]
[ 23 23 23 23 185 185 185 185 144 144 144 144]
[ 23 23 23 23 185 185 185 185 144 144 144 144]
[ 23 23 23 23 185 185 185 185 144 144 144 144]
[ 23 23 23 23 185 185 185 185 144 144 144 144]]
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
NumPy: 1.19.2
----------------------------------------

Python - How can I convert a 2D grayscale image to a 1D vector

I'm learning python and am trying to learn about manipulating images. I want to rescale (downscale) a 2D graysacle image to a 1D vector (array of single row/column). In my test code, when I rescale the image, the output values in the array are in decimal (float) format. But I want to rescale and keep the values in the 1D array as integers. Can someone please help/guide me?
This is my code:
#Testing Image to vector
#Importing required functionality
import skimage.io as io
import numpy as np
from skimage.transform import rescale
#read image
image=io.imread("https://www.usna.edu/Users/cs/wcbrown/courses/F14IC210/lab/l09/cat.jpg")
#print image
print (image)
#rescale to 50%
small_im = rescale(image,0.5)
#print the rescaled image
print(small_im)
#manipulate the array
x=np.array(small_im)
#convert to 1D vector
y=np.concatenate(x)
print (y)
#print each value in the 1D vector in a new line. Just to see how far it would go
for i in y:
print (i, end='\n')
A snippet of the output I get is this(it goes way further due to the loop):
[[ 8 8 9 ... 12 11 11]
[ 8 8 9 ... 12 11 11]
[ 7 7 8 ... 12 11 11]
...
[ 5 5 5 ... 98 97 96]
[ 5 5 5 ... 98 97 97]
[ 5 5 5 ... 99 98 97]]
[[0.02745098 0.02941176 0.02941176 ... 0.04509804 0.04313725 0.04313725]
[0.0254902 0.0254902 0.0254902 ... 0.04509804 0.04313725 0.04313725]
[0.0254902 0.0254902 0.0254902 ... 0.04509804 0.04313725 0.04313725]
...
[0.01960784 0.01960784 0.01960784 ... 0.38039216 0.37843137 0.37647059]
[0.01960784 0.01960784 0.01960784 ... 0.38039216 0.37843137 0.37647059]
[0.01960784 0.01960784 0.01960784 ... 0.38039216 0.38039216 0.37843137]]
[0.02745098 0.02941176 0.02941176 ... 0.38039216 0.38039216 0.37843137]
0.027450980392156862
0.029411764705882575
0.029411764705882575
0.027450980392156862
0.03137254901960784
0.03529411764705882
0.03529411764705882
0.032352941176470695
0.03039215686274498
0.02941176470588213
0.030392156862744994
0.03431372549019597
0.03529411764705882
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.043137254901960784
After trying and googling, I've found the answer. At least, in my context, it is what I was trying to achieve.
Solution code:
#solution to converting to 1D vector
#Importing required functionality
import numpy as np
from PIL import Image
#Opening Image and resizing to 10X10 for easy viewing
image_test = np.array(Image.open('1.png').resize((10,10))) #note: I used a local image
#print image
print (image_test)
#manipulate the array
x=np.array(image_test)
#convert to 1D vector
y=np.concatenate(x)
print (y)
#print each value in the 1D vector in a new line. Just to see how far it would go
for i in y:
print (i, end='\n')
Desired sample output (due to the loop it goes further):
[[ 48 52 72 96 96 99 81 71 68 47]
[ 52 85 133 149 168 175 157 116 70 46]
[ 54 129 170 174 185 179 177 169 92 42]
[ 55 142 165 171 187 175 162 167 97 40]
[112 150 144 134 172 157 128 143 129 113]
[162 166 166 158 166 164 154 163 157 155]
[105 166 185 174 170 165 175 179 140 81]
[ 35 113 199 170 147 145 174 181 83 32]
[ 46 65 179 183 160 153 166 155 71 37]
[ 47 58 169 178 170 159 148 158 74 39]]
[ 48 52 72 96 96 99 81 71 68 47 52 85 133 149 168 175 157 116
70 46 54 129 170 174 185 179 177 169 92 42 55 142 165 171 187 175
162 167 97 40 112 150 144 134 172 157 128 143 129 113 162 166 166 158
166 164 154 163 157 155 105 166 185 174 170 165 175 179 140 81 35 113
199 170 147 145 174 181 83 32 46 65 179 183 160 153 166 155 71 37
47 58 169 178 170 159 148 158 74 39]
48
52
72
96
96
99
81
71
68
47
52
85
133
149
168
175
157
116
70
46

How can I define if two images are similar?

I have folder with images. Some images have duplicates or similar (images of the same scene from another angle) or modifications(images which differ by size, blur level or noise filters). My task is to define if some of these images have similar images
I find this code, but I can't understand how output number describes similarity of two images when of one of them is modified or the same scene from another angle.
def compare(file1, file2):
im = [None, None] # to hold two arrays
for i, f in enumerate([file1, file2]):
im[i] = (np.array(
Image.open('C:/Users/taras/Downloads/dev_dataset/dev_dataset/'+f+'.jpg')
.convert('L') # convert to grayscale using PIL
.resize((32,32), resample=Image.BICUBIC)) # reduce size and smooth a bit using PIL
).astype(np.int) # convert from unsigned bytes to signed int using numpy
return np.abs(im[0] - im[1]).sum()
The code converts the image to greyscale and resizes it to 32x32 pixels. That means all details are lost and you just get a general idea of the colour/brightness at 1024 points regardless of the shape or size of the original image.
It then does that for the second image too and it then has 1024 brightnesses for each image. It works out the absolute difference between each pair of brightnesses by subtraction and then totals all the differences up.
If the images are identical, the differences will be zero and the result will be low. If the images are very different, they will have different brightnesses in each area and adding up those differences will come to a large number.
It is like a "Perceptual Hash" if you feel like Googling.
Here is Mr Bean and an 8x8 grey version - think of it as a vector of 64 numbers:
Here are the numbers:
255 253 255 207 124 255 254 255 255 252 255 178 67 245 255 254 255 255 255 193 154 255 255 255 255 249 183 142 192 253 251 255 255 216 92 180 156 215 254 255 255 181 96 179 115 194 255 254 255 153 95 175 92 102 246 255 255 112 98 163 97 50 195 255
Here is Paddington and an 8x8 grey version - he too is now just 64 numbers:
Here are the numbers:
247 244 166 123 114 65 0 2 223 235 163 65 30 48 20 0 218 197 59 61 110 37 32 0 140 67 14 149 183 65 7 2 57 25 64 175 169 69 0 2 51 29 57 131 112 31 3 0 60 63 59 38 14 51 32 0 59 87 61 13 11 53 46 0
Then the maths is easy:
abs(255-247) + abs(253-244) + abs(255-166) ...

Numpy 1d array subtraction - not getting expected result.

I have 2 arrays of shape (128,). I want the elementwise difference between them.
for idx, x in enumerate(test):
if idx == 0:
print (test[idx])
print()
print(library[idx])
print()
print(np.abs(np.subtract(library[idx],test[idx])))
output:
[186 3 172 80 187 120 127 172 96 213 103 107 137 119 33 53 54 113
200 78 140 234 77 94 151 64 199 218 170 73 152 73 0 5 121 42
0 106 166 80 115 220 56 66 194 187 51 132 55 73 150 83 91 204
108 58 183 0 32 240 255 55 151 255 189 153 77 89 42 176 204 170
93 117 194 195 59 204 149 55 111 255 218 48 72 171 122 163 255 155
198 179 69 173 108 0 0 176 249 214 193 255 106 116 0 47 255 255
255 255 210 175 67 0 95 120 21 158 0 72 120 255 121 208 255 0
61 255]
[189 0 178 72 177 124 123 167 81 235 110 123 139 107 39 54 34 102
195 59 156 255 66 112 161 65 180 236 181 69 142 82 0 0 152 38
0 102 146 86 117 230 59 77 220 182 44 121 63 59 146 41 92 213
146 70 184 0 0 255 255 42 165 255 245 152 114 88 63 138 255 158
96 141 221 201 47 191 179 42 156 255 237 7 136 168 133 142 254 164
236 250 56 202 141 0 0 197 255 184 212 255 108 133 0 7 255 255
255 255 243 197 74 0 50 143 24 175 0 74 101 255 121 207 255 0
146 255]
[ 3 253 6 248 246 4 252 251 241 22 7 16 2 244 6 1 236 245
251 237 16 21 245 18 10 1 237 18 11 252 246 9 0 251 31 252
0 252 236 6 2 10 3 11 26 251 249 245 8 242 252 214 1 9
38 12 1 0 224 15 0 243 14 0 56 255 37 255 21 218 51 244
3 24 27 6 244 243 30 243 45 0 19 215 64 253 11 235 255 9
38 71 243 29 33 0 0 21 6 226 19 0 2 17 0 216 0 0
0 0 33 22 7 0 211 23 3 17 0 2 237 0 0 255 0 0
85 0]
So it reads, the last array printed out is the difference between the first two arrays.
189 - 186 is 3
3 - 0 is 3 (not 253)
I must be missing something trivial.
I'd rather not zip and subtract the values as I have a ton of data.
​
Your arrays probably have dtype uint8; they cannot hold values outside the interval [0, 256), and subtracting 3 from 0 wraps around to 253. The absolute value of 253 is still 253.
Use a different dtype, or restructure your computation to avoid hitting the limits of the dtype you're using.
You can just simply subtract two numpy arrays like this, it is element-wise operation:
>test = np.array([1,2,3])
>library = np.array([1,1,1])
>np.abs(library - test)
array([0, 1, 2])

python TypeError: list indices must be integers, not list

TypeError: list indices must be integers, not list
This is the problem child;
['19 16 7 197 161 127 38 28 18 180 119 90 202 124 102 215 151 116 255 235 208 252 216 192 244 208 174 84 36 26 193 158 126 170 118 81'] <type 'list'>
Printed to a file as a string. As you can see, its a list, composed of numbers, but not identified as integers. I get the error message when the data is set to the numpy module and the corrcoef() function.
If you can help me understand this, cool, and thank you. gary
Things I have tried are :
a = [a**i for i in a1]
a = list(map(int,a1))
a = [a1[i] for i in a1]
a = [int(i) for i in a1]
I don't quite understand what you want to do with the given list, but if you want to convert it into a list of integers, you can do the following;
a = ['19 16 7 197 161 127 38 28 18 180 119 90 202 124 102 215 151 116 255 235 208 252 216 192 244 208 174 84 36 26 193 158 126 170 118 81']
a = map(int, a[0].split())
So try this:
import re
a = re.findall(r'\d+', a[0]) # regex better for parsing more complex
Or try this:
a = [int(i) for i in a.split()] # a bit simpler in this scenario

Categories

Resources