OpenCV圖像輪廓提取的實現(xiàn)
前言
常用的輪廓提取算法有:Canny、閾值分割、提取傅里葉變換的高頻信息,還有別具一格的蟻群算法,當然比較常見的作法是使用閾值分割+邊緣查找,在OpenCV里是threshold和findContours兩個函數(shù)的組合使用,和Canny。
輪廓提取的算法很多,而其目的都是為了找到圖像中灰階差比較大的位置。而所謂亞像素提取,則是使用了插值算法,以找出灰階差最大的位置。
提取傅里葉變換的高頻信息
##############
#圖像中的輪廓提取
#時間:2019/1/3
#作者:cclplus
#僅供學習交流使用
#如有疑問或者需求,可以聯(lián)系作者的郵箱
#如果你有什么好的建議或者指導(dǎo),我將不勝感激
import cv2
import numpy as np
from matplotlib import pyplot as plt
import copy
img = cv2.imread('liuyifei.jpg',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
rows,cols = img.shape
crow,ccol = int(rows/2) , int(cols/2)
for i in range(crow-30,crow+30):
for j in range(ccol-30,ccol+30):
fshift[i][j]=0.0
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)#進行高通濾波
# 取絕對值
img_back = np.abs(img_back)
plt.subplot(121),plt.imshow(img,cmap = 'gray')#因圖像格式問題,暫已灰度輸出
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
#先對灰度圖像進行伽馬變換,以提升暗部細節(jié)
rows,cols = img_back.shape
gamma=copy.deepcopy(img_back)
rows=img.shape[0]
cols=img.shape[1]
for i in range(rows):
for j in range(cols):
gamma[i][j]=5.0*pow(gamma[i][j],0.34)#0.34這個參數(shù)是我手動調(diào)出來的,根據(jù)不同的圖片,可以選擇不同的數(shù)值
#對灰度圖像進行反轉(zhuǎn)
for i in range(rows):
for j in range(cols):
gamma[i][j]=255-gamma[i][j]
plt.subplot(122),plt.imshow(gamma,cmap = 'gray')
plt.title('Result in HPF'), plt.xticks([]), plt.yticks([])
plt.show()
原圖

輸出結(jié)果

通過蟻群算法進行圖片輪廓提取
相關(guān)代碼我上傳到了我的github上
https://github.com/YuruTu/Ant_colony

效果不夠理想,這也算得上蟻群算法的一大特點,對參數(shù)要求較高,需要調(diào)參。相關(guān)內(nèi)容,筆者會持續(xù)更新
Canny邊緣檢測
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('liuyifei.jpg',0)
edges = cv2.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap='gray')
plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap='gray')
plt.title('edge'),plt.xticks([]),plt.yticks([])
plt.show()

使用cuda加速提取輪廓
#include <iostream>
#include <cuda.h>
#include <cstdlib>
#include <stdio.h>
#include <cuda_runtime.h>
#include <string>
#include <assert.h>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <device_launch_parameters.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
// GPU constant memory to hold our kernels (extremely fast access time)
__constant__ float convolutionKernelStore[256];
/**
* Convolution funcion para cuda. Destino tendra el mismo width/height como la fuente,
*
* @param source Source imagen host
* @param width Source imagen width
* @param height Source imagen height
* @param paddingX source imagen padding x
* @param paddingY source imagen padding y
* @param kOffset offset en kernel almacenamiento de memoria constante
* @param kWidth kernel width
* @param kHeight kernel height
* @param destination Imagen de destino
*/
__global__ void convolve(unsigned char *source, int width, int height, int paddingX, int paddingY, unsigned int kOffset, int kWidth, int kHeight, unsigned char *destination)
{
// Calculate our pixel's location
int x = (blockIdx.x * blockDim.x) + threadIdx.x;
int y = (blockIdx.y * blockDim.y) + threadIdx.y;
float sum = 0.0;
int pWidth = kWidth / 2;
int pHeight = kHeight / 2;
//Solo ejecuta validos pixeles
if (x >= pWidth + paddingX && y >= pHeight + paddingY && x < (blockDim.x * gridDim.x) - pWidth - paddingX &&
y < (blockDim.y * gridDim.y) - pHeight - paddingY)
{
for (int j = -pHeight; j <= pHeight; j++)
{
for (int i = -pWidth; i <= pWidth; i++)
{
// Sample the weight for this location
int ki = (i + pWidth);
int kj = (j + pHeight);
float w = convolutionKernelStore[(kj * kWidth) + ki + kOffset];
sum += w * float(source[((y + j) * width) + (x + i)]);
}
}
}
// Promedio sum
destination[(y * width) + x] = (unsigned char)sum;
}
__global__ void pythagoras(unsigned char *a, unsigned char *b, unsigned char *c)
{
int idx = (blockIdx.x * blockDim.x) + threadIdx.x;
float af = float(a[idx]);
float bf = float(b[idx]);
c[idx] = (unsigned char)sqrtf(af*af + bf * bf);
}
// crea imagen buffer
unsigned char* createImageBuffer(unsigned int bytes, unsigned char **devicePtr)
{
unsigned char *ptr = NULL;
cudaSetDeviceFlags(cudaDeviceMapHost);
cudaHostAlloc(&ptr, bytes, cudaHostAllocMapped);
cudaHostGetDevicePointer(devicePtr, ptr, 0);
return ptr;
}
int main(int argc, char** argv) {
// Abre la camaraweb
cv::VideoCapture camera(0);
cv::Mat frame;
if (!camera.isOpened())
return -1;
// capture windows
cv::namedWindow("Source");
cv::namedWindow("Greyscale");
cv::namedWindow("Blurred");
cv::namedWindow("Sobel");
// Funciones para obtener el tiempo de ejecucion
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Crea kernel gaussian(sum = 159)
const float gaussianKernel5x5[25] =
{
2.f / 159.f, 4.f / 159.f, 5.f / 159.f, 4.f / 159.f, 2.f / 159.f,
4.f / 159.f, 9.f / 159.f, 12.f / 159.f, 9.f / 159.f, 4.f / 159.f,
5.f / 159.f, 12.f / 159.f, 15.f / 159.f, 12.f / 159.f, 5.f / 159.f,
4.f / 159.f, 9.f / 159.f, 12.f / 159.f, 9.f / 159.f, 4.f / 159.f,
2.f / 159.f, 4.f / 159.f, 5.f / 159.f, 4.f / 159.f, 2.f / 159.f,
};
cudaMemcpyToSymbol(convolutionKernelStore, gaussianKernel5x5, sizeof(gaussianKernel5x5), 0);
const unsigned int gaussianKernel5x5Offset = 0;
// Sobel gradient kernels
const float sobelGradientX[9] =
{
-1.f, 0.f, 1.f,
-2.f, 0.f, 2.f,
-1.f, 0.f, 1.f,
};
const float sobelGradientY[9] =
{
1.f, 2.f, 1.f,
0.f, 0.f, 0.f,
-1.f, -2.f, -1.f,
};
cudaMemcpyToSymbol(convolutionKernelStore, sobelGradientX, sizeof(sobelGradientX), sizeof(gaussianKernel5x5));
cudaMemcpyToSymbol(convolutionKernelStore, sobelGradientY, sizeof(sobelGradientY), sizeof(gaussianKernel5x5) + sizeof(sobelGradientX));
const unsigned int sobelGradientXOffset = sizeof(gaussianKernel5x5) / sizeof(float);
const unsigned int sobelGradientYOffset = sizeof(sobelGradientX) / sizeof(float) + sobelGradientXOffset;
// Crea CPU/GPU imagenes compartidos
camera >> frame;
unsigned char *sourceDataDevice, *blurredDataDevice, *edgesDataDevice;
cv::Mat source(frame.size(), CV_8U, createImageBuffer(frame.size().width * frame.size().height, &sourceDataDevice));
cv::Mat blurred(frame.size(), CV_8U, createImageBuffer(frame.size().width * frame.size().height, &blurredDataDevice));
cv::Mat edges(frame.size(), CV_8U, createImageBuffer(frame.size().width * frame.size().height, &edgesDataDevice));
// Crea 2 imagenes temporales (sobel gradients)
unsigned char *deviceGradientX, *deviceGradientY;
cudaMalloc(&deviceGradientX, frame.size().width * frame.size().height);
cudaMalloc(&deviceGradientY, frame.size().width * frame.size().height);
// Loop while captura imagenes
while (1)
{
// Captura la imagen en escala de grises
camera >> frame;
cvtColor(frame, source, COLOR_RGB2GRAY);
_sleep(1);
// Graba el tiempo que demora el proceso
cudaEventRecord(start);
{
// convolution kernel parametros
dim3 cblocks(frame.size().width / 16, frame.size().height / 16);
dim3 cthreads(16, 16);
// pythagoran kernel parametros
dim3 pblocks(frame.size().width * frame.size().height / 256);
dim3 pthreads(256, 1);
// gaussian blur (first kernel in store @ 0)
convolve <<<cblocks, cthreads >> > (sourceDataDevice, frame.size().width, frame.size().height, 0, 0, gaussianKernel5x5Offset, 5, 5, blurredDataDevice);
// sobel gradient convolutions (x&y padding is now 2 because there is a border of 2 around a 5x5 gaussian filtered image)
convolve << <cblocks, cthreads >> > (blurredDataDevice, frame.size().width, frame.size().height, 2, 2, sobelGradientXOffset, 3, 3, deviceGradientX);
convolve << <cblocks, cthreads >> > (blurredDataDevice, frame.size().width, frame.size().height, 2, 2, sobelGradientYOffset, 3, 3, deviceGradientY);
pythagoras << <pblocks, pthreads >> > (deviceGradientX, deviceGradientY, edgesDataDevice);
cudaThreadSynchronize();
}
cudaEventRecord(stop);
// Muestra tiempo de ejecucion
float ms = 0.0f;
cudaEventSynchronize(stop);
cudaEventElapsedTime(&ms, start, stop);
std::cout << "Elapsed GPU time: " << ms << " milliseconds" << std::endl;
// Muestra resultados
imshow("Source", frame);
imshow("Greyscale", source);
imshow("Blurred", blurred);
imshow("Sobel", edges);
// Spin
if (cv::waitKey(1) == 27) break;
}
// Exit
cudaFreeHost(source.data);
cudaFreeHost(blurred.data);
cudaFreeHost(edges.data);
cudaFree(deviceGradientX);
cudaFree(deviceGradientY);
return 0;
}
很多時候加上Cuda是有必要的,如果你要使用hough變換之類的時間復(fù)雜度比較高的代碼,Gpu編程會給你帶來多個數(shù)量級的加速。
到此這篇關(guān)于OpenCV圖像輪廓提取的實現(xiàn)的文章就介紹到這了,更多相關(guān)OpenCV圖像輪廓提取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中隊列的結(jié)構(gòu)和函數(shù)接口的使用示例
隊列只允許一端進行插入數(shù)據(jù)操作,在另一端進行刪除數(shù)據(jù)操作的特殊線性表,隊列具有先進先出FIFO的性質(zhì);隊列可用數(shù)組和鏈表 的方法實現(xiàn),使用鏈表的結(jié)構(gòu)實現(xiàn)更優(yōu)一些,因為如果使用數(shù)組節(jié),出隊列時刪去首元素需要將整個數(shù)組前移,效率比較低2023-02-02
C語言數(shù)據(jù)結(jié)構(gòu)線性表教程示例詳解
這篇文章主要為大家介紹了C語言數(shù)據(jù)結(jié)構(gòu)線性表的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-02-02
string中c_str(),data(),copy(p,n)函數(shù)的用法總結(jié)
以下是對string中c_str(),data(),copy(p,n)函數(shù)的用法進行了詳細的介紹,需要的朋友可以過來參考下2013-09-09
C++?LeetCode1827題解最少操作使數(shù)組遞增
這篇文章主要為大家介紹了C++?LeetCode1827題解最少操作使數(shù)組遞增示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
《C++ Primer》隱式類類型轉(zhuǎn)換學習整理
在本篇文章里小編給大家整理的是關(guān)于《C++ Primer》隱式類類型轉(zhuǎn)換學習筆記內(nèi)容,需要的朋友們參考下。2020-02-02

