matlab/python+opencv提取圆形鱼眼图片的有效区域
2016-11-28 09:40
836 查看
1、原图:
2、有效区域:
3、matlab代码:
4、python+opencv代码:
# -*- coding: utf-8 -*-
from __future__ import division
import cv2
#读取鱼眼图片
img = cv2.imread("fisheye.jpg")
#设置灰度阈值
T = 40
#转换为灰度图片
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#提取原图大小
rows,cols = img.shape[:2]
print rows,cols
#从上向下扫描
for i in range(0,rows,1):
for j in range(0,cols,1):
if img_gray[i,j] >= T:
if img_gray[i + 1,j] >= T:
top = i
break
else:
continue
break
print 'top =',top
#从下向上扫描
for i in range(rows-1,-1,-1):
for j in range(0,cols,1):
if img_gray[i,j] >= T:
if img_gray[i - 1,j] >= T:
bottom = i
break
else:
continue
break
print 'bottom =',bottom
#从左向右扫描
for j in range(0,cols,1):
for i in range(top,bottom,1):
if img_gray[i,j] >= T:
if img_gray[i,j + 1] >= T:
left = j
break
else:
continue
break
print 'left =',left
#从右向左扫描
for j in range(cols-1,-1,-1):
for i in range(top,bottom,1):
if img_gray[i,j] >= T:
if img_gray[i,j - 1] >= T:
right = j
break
else:
continue
break
print 'right =',right
#计算有效区域半径
R = max((bottom - top) / 2,(right - left) / 2)
print 'R =',R
#提取有效区域
img_valid = img[top:top + 2 * R,left:left + 2 * R]
cv2.imwrite('fisheye_valid.jpg',img_valid)
#显示图片
cv2.imshow('fisheye',img)
cv2.imshow("fisheye_valid",img_valid)
cv2.waitKey(0)
cv2.destroyAllWindows()
2、有效区域:
3、matlab代码:
close all;clear;clc %读取原图片 img=imread('fisheye.jpg'); %设置灰度阈值 T=40; %转为灰度图片 img_gray=rgb2gray(img); %提取原图大小 [m,n,k]=size(img_gray); %从上向下扫描 for i=1:m flag=0; for j=1:n if(img_gray(i,j)>=T) if(img_gray(i+1,j)>=T) top=i; flag=1; break; end end end if flag==1 break; end end %从下向上扫描 for i=m:-1:1 flag=0; for j=1:n if(img_gray(i,j)>=T) if(img_gray(i-1,j)>=T) bottom=i; flag=1; break; end end end if flag==1 break; end end %从左向右扫描 for j=1:n flag=0; for i=top:bottom if(img_gray(i,j)>=T) if(img_gray(i,j+1)>=T) left=j; flag=1; break; end end end if flag==1 break; end end %从右向左扫描 for j=n:-1:1 flag=0; for i=top:bottom if(img_gray(i,j)>=T) if(img_gray(i,j-1)>=T) right=j; flag=1; break; end end end if flag==1 break; end end %计算鱼眼图像有效区域半径 R=max((right-left)/2,(bottom-top)/2); %生成有效区域内新的鱼眼图像 img_valid=imcrop(img,[left,top,2*R,2*R]); %保存有效区域图片 imwrite(img_valid,'img_valid.jpg'); %显示图片 figure(1);imshow(img); figure(2);imshow(img_valid);
4、python+opencv代码:
# -*- coding: utf-8 -*-
from __future__ import division
import cv2
#读取鱼眼图片
img = cv2.imread("fisheye.jpg")
#设置灰度阈值
T = 40
#转换为灰度图片
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#提取原图大小
rows,cols = img.shape[:2]
print rows,cols
#从上向下扫描
for i in range(0,rows,1):
for j in range(0,cols,1):
if img_gray[i,j] >= T:
if img_gray[i + 1,j] >= T:
top = i
break
else:
continue
break
print 'top =',top
#从下向上扫描
for i in range(rows-1,-1,-1):
for j in range(0,cols,1):
if img_gray[i,j] >= T:
if img_gray[i - 1,j] >= T:
bottom = i
break
else:
continue
break
print 'bottom =',bottom
#从左向右扫描
for j in range(0,cols,1):
for i in range(top,bottom,1):
if img_gray[i,j] >= T:
if img_gray[i,j + 1] >= T:
left = j
break
else:
continue
break
print 'left =',left
#从右向左扫描
for j in range(cols-1,-1,-1):
for i in range(top,bottom,1):
if img_gray[i,j] >= T:
if img_gray[i,j - 1] >= T:
right = j
break
else:
continue
break
print 'right =',right
#计算有效区域半径
R = max((bottom - top) / 2,(right - left) / 2)
print 'R =',R
#提取有效区域
img_valid = img[top:top + 2 * R,left:left + 2 * R]
cv2.imwrite('fisheye_valid.jpg',img_valid)
#显示图片
cv2.imshow('fisheye',img)
cv2.imshow("fisheye_valid",img_valid)
cv2.waitKey(0)
cv2.destroyAllWindows()
相关文章推荐
- 【Python+OpenCV】图片局部区域像素值处理(改进版)-一种特征提取方法
- 快速扫描算法提取鱼眼图像有效区域
- 条形码区域的提取--python(opencv)
- OpenCV_提取图片中任意形状的区域
- opencv3+python3.5成语填字游戏(二)填字图片汉字提取和识别
- (转载)OpenCV_提取图片中任意形状的区域
- python+opencv识别图片中的圆形
- OpenCV_提取图片中任意形状的区域
- 已知图片某区域内所有点坐标,求区域周长(非opencv轮廓提取)
- 快速扫描算法提取鱼眼图像有效区域
- 逐行逐列扫描算法提取鱼眼图像有效区域
- 【Python_Demo_4】 利用OpenCv的ROI区域,将衣服图片切成规则的小图片的python程序
- Python+OpenCV感兴趣区域ROI提取
- MATLAB-图片特定颜色区域提取(矩阵)
- 【Python+OpenCV】视频流局部区域像素值处理-一种特征提取方法
- 【Python+OpenCV】图片局部区域像素值处理
- opencv2读取视频提取ROI区域并把ROI区域保存为图片
- 区域生长算法提取鱼眼有效区域
- opencv for python (6) 改变一幅图的特定区域 (往一幅图片上加标志)
- OpenCV_提取图片中任意形状的区域