lvyunxiang

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdio.h"


/*int main()
{
printf("test");
return 0;
}
*/
#include <opencv2/core/core.hpp>  
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>  
using namespace cv;


void templateMatching(const Mat& srcImage, const Mat& templateImage)
{
    Mat result;
    int result_cols = srcImage.cols - templateImage.cols + 1;
    int result_rows = srcImage.rows - templateImage.rows + 1;
    if (result_cols < 0 || result_rows < 0)
    {
        //qDebug() << "Please input correct image!";
        return;
    }
    result.create(result_cols, result_rows, CV_32FC1);
    //    enum { TM_SQDIFF=0, TM_SQDIFF_NORMED=1, TM_CCORR=2, TM_CCORR_NORMED=3, TM_CCOEFF=4, TM_CCOEFF_NORMED=5 };
    matchTemplate(srcImage, templateImage, result, TM_CCOEFF_NORMED);   //最好匹配为1,值越小匹配越差
    double minVal = -1;
    double maxVal;
    Point minLoc;
    Point maxLoc;
    Point matchLoc;
    minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());

    //取大值(视匹配方法而定)
    //    matchLoc = minLoc;
    matchLoc = maxLoc;

    //取大值,值越小表示越匹配
    //QString str = "Similarity:" + QString::number((maxVal) * 100, \'f\', 2) + "%";
    printf("%f\n", maxVal * 100);
    //qDebug(str.toAscii().data());

    Mat mask = srcImage.clone();
    //绘制最匹配的区域
    rectangle(mask, matchLoc, Point(matchLoc.x + templateImage.cols, matchLoc.y + templateImage.rows), Scalar(0, 255, 0), 2, 8, 0);
    imshow("mask", mask);
}

int main()
{
    //namedWindow("Example2", WINDOW_AUTOSIZE);
    VideoCapture cap;
    cap.open("C:\\Users\\Administrator\\Desktop\\1555554426020.ts");
    Mat frame;
    Mat logo = imread("C:\\Users\\Administrator\\Desktop\\20200409143249.png");
    while (1)
    {
        cap >> frame;
        //resize(frame, frame,cv::Size(1366,768), 0, 0);
        if (frame.empty())
            //break;
            continue;

        //imshow("Exameple2", frame);
        //imshow("logo", logo);
        //if (waitKey(33) >= 0) break;
        templateMatching(frame, logo);
        //waitKey(33);
        waitKey(2);
    }
    


    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

rcnn mask

import os
import sys
import tarfile

import cv2 as cv
import numpy as np
import tensorflow as tf
from object_detection.utils import ops as utils_ops

from utils import label_map_util
from utils import visualization_utils as vis_util

# What model to download.
MODEL_NAME = \'mask_rcnn_inception_v2_coco_2018_01_28\'
MODEL_FILE = \'F:/tensorflow_object_detech/\' + MODEL_NAME + \'.tar.gz\'

# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_FROZEN_GRAPH = MODEL_NAME + \'/frozen_inference_graph.pb\'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join(\'F:/tensorflow_object_detech/models-master/research/object_detection/data/\', \'mscoco_label_map.pbtxt\')

NUM_CLASSES = 90
#capture = cv.VideoCapture(0)
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():
file_name = os.path.basename(file.name)
if \'frozen_inference_graph.pb\' in file_name:
tar_file.extract(file, os.getcwd())

detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
#od_graph_def = tf.compat.v1.GraphDef
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, \'rb\') as fid:
#with tf.io.gfile.GFile(PATH_TO_FROZEN_GRAPH, \'rb\') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name=\'\')

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categorys = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categorys)


def run_inference_for_single_image(image, graph):
with graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
\'num_detections\', \'detection_boxes\', \'detection_scores\',
\'detection_classes\', \'detection_masks\'
]:
tensor_name = key + \':0\'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
if \'detection_masks\' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict[\'detection_boxes\'], [0])
detection_masks = tf.squeeze(tensor_dict[\'detection_masks\'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict[\'num_detections\'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
# Follow the convention by adding back the batch dimension
tensor_dict[\'detection_masks\'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name(\'image_tensor:0\')

# Run inference
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})

# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict[\'num_detections\'] = int(output_dict[\'num_detections\'][0])
output_dict[\'detection_classes\'] = output_dict[
\'detection_classes\'][0].astype(np.uint8)
output_dict[\'detection_boxes\'] = output_dict[\'detection_boxes\'][0]
output_dict[\'detection_scores\'] = output_dict[\'detection_scores\'][0]
if \'detection_masks\' in output_dict:
output_dict[\'detection_masks\'] = output_dict[\'detection_masks\'][0]
return output_dict

image = cv.imread("D:/3323c544a8e7be61e3c7fb36e55a5d42.jpeg")
cv.imshow("input", image)
print(image.shape)

output_dict = run_inference_for_single_image(image, detection_graph)



boxes = output_dict[\'detection_boxes\']
scores = output_dict[\'detection_scores\']
classes = output_dict[\'detection_classes\']
num_detections = output_dict[\'num_detections\']
im_width = image.shape[1]
im_height = image.shape[0]
count = 0
for i in range(100):
if scores is None or scores[i] > 0.1:
count = count + 1
for i in range(count):
y_min = boxes[i][0] * im_height
x_min = boxes[i][1] * im_width
y_max = boxes[i][2] * im_height
x_max = boxes[i][3] * im_width
#print("object{0}: {1}".format(i, category_index[classes[0][i]][\'name\']),\',Center_X:\', int((x_min + x_max) / 2), \',Center_Y:\', int((y_min + y_max) / 2))
print("object{0}: {1}______".format(i, category_index[classes[i]][\'name\']),"xmin:",x_min," ymin",y_min," xmax",x_max,"ymax",y_max)


vis_util.visualize_boxes_and_labels_on_image_array(
image,
output_dict[\'detection_boxes\'],
output_dict[\'detection_classes\'],
output_dict[\'detection_scores\'],
category_index,
instance_masks=output_dict.get(\'detection_masks\'),
min_score_thresh=0.5,
use_normalized_coordinates=True,
line_thickness=4
)

cv.imshow("mask rcnn demo", image)
cv.waitKey(0)
cv.destroyAllWindows()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//tensorflow ssd 模型检测

import os
import sys
import tarfile
import cv2 as cv
import numpy as np
import tensorflow as tf
from utils import label_map_util
from utils import visualization_utils as vis_util
# What model to download.
MODEL_NAME = \'ssd_mobilenet_v1_coco_2018_01_28\'
MODEL_FILE = \'F:/tensorflow_object_detech/\' + MODEL_NAME + \'.tar.gz\'

# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_FROZEN_GRAPH = MODEL_NAME + \'/frozen_inference_graph.pb\'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join(\'F:/tensorflow_object_detech/models-master/research/object_detection/data\', \'mscoco_label_map.pbtxt\')

NUM_CLASSES = 90
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():
file_name = os.path.basename(file.name)
if \'frozen_inference_graph.pb\' in file_name:
tar_file.extract(file, os.getcwd())

detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, \'rb\') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name=\'\')

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categorys = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categorys)

with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
image = cv.imread("D:/3323c544a8e7be61e3c7fb36e55a5d42.jpeg")
cv.imshow("input", image)
print(image.shape)
image_np_expanded = np.expand_dims(image, axis=0)
image_tensor = detection_graph.get_tensor_by_name(\'image_tensor:0\')
boxes = detection_graph.get_tensor_by_name(\'detection_boxes:0\')
scores = detection_graph.get_tensor_by_name(\'detection_scores:0\')
classes = detection_graph.get_tensor_by_name(\'detection_classes:0\')
num_detections = detection_graph.get_tensor_by_name(\'num_detections:0\')

(boxes, scores, classes, num_detections) = sess.run([boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
print("score")
print(scores)
print("num_detections=")
print(num_detections)
#print("boxes")
#print(boxes)
#(im_width, im_height) = image.size
im_width = image.shape[1]
im_height = image.shape[0]
count = 0
for i in range(100):
if scores is None or scores[0][i] > 0.1:
count = count + 1
for i in range(count):
y_min = boxes[0][i][0] * im_height
x_min = boxes[0][i][1] * im_width
y_max = boxes[0][i][2] * im_height
x_max = boxes[0][i][3] * im_width
#print("object{0}: {1}".format(i, category_index[classes[0][i]][\'name\']),\',Center_X:\', int((x_min + x_max) / 2), \',Center_Y:\', int((y_min + y_max) / 2))
print("object{0}: {1}______".format(i, category_index[classes[0][i]][\'name\']),"xmin:",x_min," ymin",y_min," xmax",x_max,"ymax",y_max)
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
min_score_thresh=0.1,
use_normalized_coordinates=True,
line_thickness=4
)
cv.imshow("SSD - object detection image", image)
cv.waitKey(0)
cv.destroyAllWindows()
 

分类:

技术点:

相关文章: