每天进步一点点------如何实现Sobel Edge Detector? (Image Processing) (C/C++)
使用C與C++/CLI實現Sobel Edge Detector。
http://www.cnblogs.com/oomusou/archive/2008/07/23/sobel_edge_detector.html
/*
(C) OOMusou 2007 http://oomusou.cnblogs.com Filename : sobel_edge.c
Compiler : Visual C++ 8.0
Description : Demo the how to use sobel detector on gray level image
Release : 07/23/2008 1.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h> #define MASK_N 2
#define MASK_X 3
#define MASK_Y 3
#define WHITE 255
#define BLACK 0 unsigned char *image_s = NULL; // source image array
unsigned char *image_t = NULL; // target image array
FILE *fp_s = NULL; // source file handler
FILE *fp_t = NULL; // target file handler unsigned int width, height; // image width, image height
unsigned int rgb_raw_data_offset;// RGB raw data offset
unsigned char bit_per_pixel; // bit per pixel
unsigned short byte_per_pixel; // byte per pixel // bitmap header
unsigned char header[] = {
0x42, // identity : B
0x4d, // identity : M
, , , , // file size
, , // reserved1
, , // reserved2
, , , , // RGB data offset
, , , , // struct BITMAPINFOHEADER size
, , , , // bmp width
, , , , // bmp height
, , // planes
, , // bit per pixel
, , , , // compression
, , , , // data size
, , , , // h resolution
, , , , // v resolution
, , , , // used colors
, , , // important colors
}; // sobel mask
int mask[MASK_N][MASK_X][MASK_Y] = {
{{-,-,-},
{ , , },
{ , , }}, {{-, , },
{-, , },
{-, , }}
}; int read_bmp(const char *fname_s) {
fp_s = fopen(fname_s, "rb");
if (fp_s == NULL) {
printf("fopen fp_s error\n");
return -;
} // move offset to 10 to find rgb raw data offset
fseek(fp_s, , SEEK_SET);
fread(&rgb_raw_data_offset, sizeof(unsigned int), , fp_s); // move offset to 18 to get width & height;
fseek(fp_s, , SEEK_SET);
fread(&width, sizeof(unsigned int), , fp_s);
fread(&height, sizeof(unsigned int), , fp_s); // get bit per pixel
fseek(fp_s, , SEEK_SET);
fread(&bit_per_pixel, sizeof(unsigned short), , fp_s);
byte_per_pixel = bit_per_pixel / ; // move offset to rgb_raw_data_offset to get RGB raw data
fseek(fp_s, rgb_raw_data_offset, SEEK_SET); image_s = (unsigned char *)malloc((size_t)width * height * byte_per_pixel);
if (image_s == NULL) {
printf("malloc images_s error\n");
return -;
} image_t = (unsigned char *)malloc((size_t)width * height * byte_per_pixel);
if (image_t == NULL) {
printf("malloc image_t error\n");
return -;
} fread(image_s, sizeof(unsigned char), (size_t)(long)width * height * byte_per_pixel, fp_s); return ;
} // convert RGB to gray level int
int color_to_int(int r, int g, int b) {
return (r + g + b) / ;
} int sobel(double threshold) {
unsigned int x, y, i, v, u; // for loop counter
unsigned char R, G, B; // color of R, G, B
double val[MASK_N] = {0.0};
int adjustX, adjustY, xBound, yBound;
double total; for(y = ; y != height; ++y) {
for(x = ; x != width; ++x) {
for(i = ; i != MASK_N; ++i) {
adjustX = (MASK_X % ) ? : ;
adjustY = (MASK_Y % ) ? : ;
xBound = MASK_X / ;
yBound = MASK_Y / ; val[i] = 0.0;
for(v = -yBound; v != yBound + adjustY; ++v) {
for (u = -xBound; u != xBound + adjustX; ++u) {
if (x + u >= && x + u < width && y + v >= && y + v < height) {
R = *(image_s + byte_per_pixel * (width * (y+v) + (x+u)) + );
G = *(image_s + byte_per_pixel * (width * (y+v) + (x+u)) + );
B = *(image_s + byte_per_pixel * (width * (y+v) + (x+u)) + ); val[i] += color_to_int(R, G, B) * mask[i][u + xBound][v + yBound];
}
}
}
} total = 0.0;
for (i = ; i != MASK_N; ++i) {
total += val[i] * val[i];
} total = sqrt(total); if (total - threshold >= ) {
// black
*(image_t + byte_per_pixel * (width * y + x) + ) = BLACK;
*(image_t + byte_per_pixel * (width * y + x) + ) = BLACK;
*(image_t + byte_per_pixel * (width * y + x) + ) = BLACK;
}
else {
// white
*(image_t + byte_per_pixel * (width * y + x) + ) = WHITE;
*(image_t + byte_per_pixel * (width * y + x) + ) = WHITE;
*(image_t + byte_per_pixel * (width * y + x) + ) = WHITE;
}
}
} return ;
} int write_bmp(const char *fname_t) {
unsigned int file_size; // file size fp_t = fopen(fname_t, "wb");
if (fp_t == NULL) {
printf("fopen fname_t error\n");
return -;
} // file size
file_size = width * height * byte_per_pixel + rgb_raw_data_offset;
header[] = (unsigned char)(file_size & 0x000000ff);
header[] = (file_size >> ) & 0x000000ff;
header[] = (file_size >> ) & 0x000000ff;
header[] = (file_size >> ) & 0x000000ff; // width
header[] = width & 0x000000ff;
header[] = (width >> ) & 0x000000ff;
header[] = (width >> ) & 0x000000ff;
header[] = (width >> ) & 0x000000ff; // height
header[] = height &0x000000ff;
header[] = (height >> ) & 0x000000ff;
header[] = (height >> ) & 0x000000ff;
header[] = (height >> ) & 0x000000ff; // bit per pixel
header[] = bit_per_pixel; // write header
fwrite(header, sizeof(unsigned char), rgb_raw_data_offset, fp_t); // write image
fwrite(image_t, sizeof(unsigned char), (size_t)(long)width * height * byte_per_pixel, fp_t); fclose(fp_s);
fclose(fp_t); return ;
} int main() {
read_bmp("lena.bmp"); // 24 bit gray level image
sobel(90.0);
write_bmp("lena_sobel.bmp");
}
/*
(C) OOMusou 2007 http://oomusou.cnblogs.com Filename : sobel_edge.cpp
Compiler : C++/CLI / Visual C++ 8.0
Description : Demo the how to use sobel detector on gray level image
Release : 07/23/2008 1.0
*/ #include "stdafx.h"
#include <cmath> using namespace System::Drawing;
using namespace System::Drawing::Imaging; const int MASK_N = ;
const int MASK_X = ;
const int MASK_Y = ; // Convert RGB to gray level int
int colorToInt(Color %color) {
return (color.R + color.G + color.B) / ;
} void edgeDetector(Bitmap^ oriImg, Bitmap^ resImg, const int mask[MASK_N][MASK_X][MASK_Y], const double &threshold) {
double val[MASK_N] = {0.0}; for(int y = ; y != oriImg->Height; ++y) {
for(int x = ; x != oriImg->Width; ++x) {
for(int i = ; i != MASK_N; ++i) {
int adjustX = (MASK_X % ) ? : ;
int adjustY = (MASK_Y % ) ? : ;
int xBound = MASK_X / ;
int yBound = MASK_Y / ; val[i] = 0.0;
for(int v = -yBound; v != yBound + adjustY; ++v) {
for (int u = -xBound; u != xBound + adjustX; ++u) {
if (x + u >= && x + u < oriImg->Width && y + v >= && y + v < oriImg->Height) {
val[i] += colorToInt(oriImg->GetPixel(x + u, y + v)) * mask[i][u + xBound][v + yBound];
}
}
}
} double total = 0.0;
for (int i = ; i != MASK_N; ++i) {
total += val[i] * val[i];
} total = sqrt(total); if (total - threshold >= )
resImg->SetPixel(x , y, Color::Black);
else
resImg->SetPixel(x, y, Color::White);
}
}
} int main() {
const int mask[MASK_N][MASK_X][MASK_Y] = {
{{-,-,-},
{ , , },
{ , , }}, {{-, , },
{-, , },
{-, , }}
}; Bitmap^ oriImg = gcnew Bitmap("lena.bmp");
Bitmap^ resImg = gcnew Bitmap(oriImg->Width, oriImg->Height); const double threshold = 90.0;
edgeDetector(oriImg, resImg, mask, threshold); resImg->Save("lena_sobel.bmp"); return ;
}
每天进步一点点------如何实现Sobel Edge Detector? (Image Processing) (C/C++)的更多相关文章
- Canny Edge Detector
Canny边缘检测算法有自己的理论和经验性的推导, 没仔细看/没看明白. 它的步骤如下: 对原图的灰度图进行高斯滤波 求一阶导数, 得到每个像素点的梯度强度和方向. 非最大抑制. 对每个edge ca ...
- Understanding Convolution in Deep Learning
Understanding Convolution in Deep Learning Convolution is probably the most important concept in dee ...
- 32个最热CPLD-FPGA论坛
1. OPENCORES.ORG这里提供非常多,非常好的PLD了内核,8051内核就可以在里面找到.进入后,选择project或者由http//www.opencores.org/browse.cgi ...
- [转]FPGA网站推荐
1. OPENCORES.ORG这里提供非常多,非常好的PLD了内核,8051内核就可以在里面找到.进入后,选择project或者由http//www.opencores.org/browse.cgi ...
- Computer Vision_33_SIFT:Remote Sensing Image Registration With Modified SIFT and Enhanced Feature Matching——2017
此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...
- OpenCV2马拉松第14圈——边缘检測(Sobel,prewitt,roberts)
收入囊中 差分在边缘检測的角色 Sobel算子 OpenCV sobel函数 OpenCV Scharr函数 prewitt算子 Roberts算子 葵花宝典 差分在边缘检測究竟有什么用呢?先看以下的 ...
- Sobel Derivatives
https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html ...
- 线特征---Edge Drawing(七)
http://ceng.anadolu.edu.tr/cv/edgedrawing/ References C. Topal, C. Akinlar, Edge Drawing: A Combined ...
- 2.7 Sobel导数
OpenCV函数 Sobel(src_gray,grad_x/grad_y,ddepth,x_order,y_order,scale,delta,BORDER_DEFAULT ) Scharr( ) ...
随机推荐
- [TJOI2013] 攻击装置 - 二分图匹配
给定 \(N \times N\) 棋盘,某些格子是障碍,问可以放置的互不侵犯的马的个数 黑白染色后建立二分图,求最大独立集 = 总点数 - 最大匹配数 注意把反边也连上会WA掉(脑抽一发血) #in ...
- HBuilderX开发app实现自动更新版本
需求说明:使用MUI+Vue等技术并且通过HBuilderX打包开发移动app,在有版本更新时需要自动提示用户有新版本,并且可以点击下载自动安装. 思路说明: 应用打开时(使用Vue的生命周期mo ...
- 【vue】 vue跳转页面:router-link/this.$router.push()
1.通过标签<router-link> <router-link to='A'>跳转到A页面</router-link> 2.通过方法 this.$router.p ...
- AntDesign(React)学习-2 第一个页面
1.前面创建了第一个项目jgdemo,结构如下,使用TypeScript. 2.yarn start启动项目 3.点击GettingStarted是umi的官方网站 https://umijs.org ...
- Java基础知识点总结笔记
面向对象的三大特性 继承:一般只能单继承,内部类实现多继承,接口可以多继承 封装:访问权限控制public>protected>包>private内部类也是一种封装 多态:编译时多态 ...
- python面试的100题(13)
29.Given an array of integers 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用.示例:给定nums ...
- 使用shader,矩阵旋转实现图片的旋转动画
常用于loading动画之类的 具体的实现代码: fixed4 frag (v2f i) : SV_Target { //1.先将uv平移到原点(让图片中心与原点重合) float2 pianyi=( ...
- 一、c++语言基础
1. 程序员的第一条代码"Hello,world!" #include <cstdio> //头文件,主要负责输入.输出 using namespace std;//C ...
- phpstorm汉化包
链接:https://pan.baidu.com/s/1dG7AWI87dOJJezra9veFrA 提取码:btmf 下载后放到lib目录中 即可
- Codeforces Round #601 (Div. 2) A Changing Volume
好吧,其实我拿到这个题的时候,首先想到了bfs,写完之后,开开森森的去交代码,却在第二个数据就TEL,然后优化半天,还是不行. 最终,我盯着1,2,5发呆半天,wc,然后直接贪心 #include&l ...