简体   繁体   English

机器学习图像特征提取

[英]Machine learning image feature extraction

There is a problem about feature extraction from grayscale image in machine learning.机器学习中存在从灰度图像中提取特征的问题。

I have a gray image converted from colored with this.我有一个从彩色转换而来的灰色图像。

from PIL import Image
img = Image.open('source.png').convert('LA')
img.save('greyscalesource.png')

image2 = imread('greyscalesource.png')
print("The type of this input is {}".format(type(image)))
print("Shape: {}".format(image2.shape))
plt.imshow(image2)

output is: output 是: 在此处输入图像描述

I actually need to feature extraction from this gray picture because next part is about train a model with this feature for predict to colorized form of an image.我实际上需要从这张灰色图片中提取特征,因为下一部分是关于训练具有此特征的 model 以预测图像的彩色形式。

We can't use any deep learning library我们不能使用任何深度学习库

There are some of methods such as SIFT ORB FAST... But I really don't know how can extract features for my aim.有一些方法,如 SIFT ORB FAST ......但我真的不知道如何为我的目标提取特征。

#ORB
orb = cv2.ORB_create()
#keypoints and descriptors
kpO, desO = orb.detectAndCompute(img, None)
img7 = cv2.drawKeypoints(img, kpO, 1, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imwrite('_ORB.jpg',img7)

Output of above code is just True.上述代码的 Output 是正确的。

Is there any solution or idea what should I do?有什么解决方案或想法我应该怎么做?

The descriptor des0 in your line:您的行中的描述符des0

kpO, desO = orb.detectAndCompute(img, None)

is the feature you need to use for ML algorithm.是您需要用于 ML 算法的功能。

Below is an example of Dense SIFT-based matching on a stereo image pair using ML's knn algo:下面是使用 ML 的 knn 算法对立体图像对进行基于密集 SIFT 的匹配的示例:

Input Image:输入图像: 在此处输入图像描述

Read input image and split stereo image读取输入图像并分割立体图像

import cv2
import matplotlib.pyplot as plt
import numpy as np

def split_input_image(im):
    im1 = im[:,:int(im.shape[1]/2)]
    im2 = im[:,int(im.shape[1]/2):im.shape[1]]
    # Convert to grayscale
    g_im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
    g_im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
    return im1, im2, g_im1, g_im2

im = cv2.imread('../input_data/Stereo_Pair.jpg')
im1, im2, g_im1, g_im2 = split_input_image(im)

Write function for dense sift写 function 进行密集筛选

def dense_sift(gray_im):
    sift = cv2.xfeatures2d.SIFT_create()
    step_size = 5
    kp = [cv2.KeyPoint(x,y,step_size) for y in range(0,gray_im.shape[0],step_size)
                                      for x in range(0,gray_im.shape[1],step_size)]
    k,feat = sift.compute(gray_im,kp) # keypoints and features
    return feat, kp

Create an empty template image of similar dimensions to vizualize sift matches创建相似尺寸的空模板图像以可视化筛选匹配

visualize_sift_matches = np.zeros([im1.shape[0],im1.shape[1]])

Get features and key-points for gray-scale images (my order is reversed. don't get confused.)获取灰度图像的特征和关键点(我的顺序是颠倒的。不要混淆。)

f1, kp1 = dense_sift(g_im1)
f2, kp2 = dense_sift(g_im2)

Get matches from two feature sets using kNN使用 kNN 从两个特征集中获取匹配

bf = cv2.BFMatcher()
matches = bf.knnMatch(f1,f2,k=2)

Find common matches for a min threshold查找最小阈值的常见匹配项

common_matches = []
for m,n in matches:
    if m.distance < 0.8 * n.distance:
        common_matches.append([m])

Juxtapose the two images and Connect the key-points并列两个图像并连接关键点

visualize_sift_matches = cv2.drawMatchesKnn(im1, kp1, im2, kp2, common_matches,
visualize_sift_matches, flags=2)

Visualize可视化

plt.imshow(visualize_sift_matches)
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM