问答1 问答5 问答50 问答500 问答1000

求高手 帮帮忙,用vc编程利用sobel算子对图像进行边缘检测

提问网友 发布时间:2022-04-23 18:06
声明声明:本网页内容为用户发布,旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:1656858193@qq.com
1个回答
热心网友 回答时间:2023-10-12 05:50
/*
FILE: edgeSob.c - WORKS!!
AUTH: Bill Green
DESC: 2 3x3 Sobel masks for edge detection
DATE: 07/23/02
REFS: edgeLap.c
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <alloc.h>

/*-------STRUCTURES---------*/
typedef struct {int rows; int cols; unsigned char* data;} sImage;

/*-------PROTOTYPES---------*/
long getImageInfo(FILE*, long, int);
void copyImageInfo(FILE* inputFile, FILE* outputFile);
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors);

int main(int argc, char* argv[])
{
FILE *bmpInput, *bmpOutput;
sImage originalImage;
sImage edgeImage;
unsigned int X, Y;
int I, J;
long sumX, sumY;
int nColors, SUM;
unsigned long vectorSize;
unsigned long fileSize;
int GX[3][3];
int GY[3][3];
unsigned char *pChar, someChar;
unsigned int row, col;

someChar = '0'; pChar = &someChar;

/* 3x3 GX Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html */
GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1;
GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2;
GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;

/* 3x3 GY Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html */
GY[0][0] = 1; GY[0][1] = 2; GY[0][2] = 1;
GY[1][0] = 0; GY[1][1] = 0; GY[1][2] = 0;
GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1;

if(argc < 2) {
printf("Usage: %s bmpInput.bmp\n", argv[0]);
exit(0);
};
printf("Reading filename %s\n", argv[1]);

/*-------DECLARE INPUT & OUTPUT FILES-------*/
bmpInput = fopen(argv[1], "rb");
bmpOutput = fopen("edgeSob.bmp", "wb");

/*---SET POINTER TO BEGINNING OF FILE----*/
fseek(bmpInput, 0L, SEEK_END);

/*-------GET INPUT BMP DATA--------*/
fileSize = getImageInfo(bmpInput, 2, 4);
originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
edgeImage.rows = originalImage.rows;
edgeImage.cols = originalImage.cols;

/*--------PRINT DATA TO SCREEN----------*/
printf("Width: %d\n", originalImage.cols);
printf("Height: %d\n", originalImage.rows);
printf("File size: %lu\n", fileSize);

nColors = (int)getImageInfo(bmpInput, 46, 4);
printf("nColors: %d\n", nColors);

/*------ALLOCATE MEMORY FOR FILES--------*/
vectorSize = fileSize - (14+40+4*nColors);
printf("vectorSize: %lu\n", vectorSize);
edgeImage.data = farmalloc(vectorSize*sizeof(unsigned char));
if(edgeImage.data == NULL) {
printf("Failed to malloc edgeImage.data\n");
exit(0);
}
printf("%lu bytes malloc'ed for edgeImage.data\n", vectorSize);

originalImage.data = farmalloc(vectorSize*sizeof(unsigned char));
if(originalImage.data == NULL) {
printf("Failed to malloc originalImage.data\n");
exit(0);
}
printf("%lu bytes malloc'ed for originalImage.datt\n", vectorSize);

/*------COPY HEADER AND COLOR TABLE---------*/
copyImageInfo(bmpInput, bmpOutput);
copyColorTable(bmpInput, bmpOutput, nColors);
fseek(bmpInput, (14+40+4*nColors), SEEK_SET);
fseek(bmpOutput, (14+40+4*nColors), SEEK_SET);

/* Read input.bmp and store it's raster data into originalImage.data */
for(row=0; row<=originalImage.rows-1; row++) {
for(col=0; col<=originalImage.cols-1; col++) {
fread(pChar, sizeof(char), 1, bmpInput);
*(originalImage.data + row*originalImage.cols + col) = *pChar;
}
}

/*---------------------------------------------------
SOBEL ALGORITHM STARTS HERE
---------------------------------------------------*/
for(Y=0; Y<=(originalImage.rows-1); Y++) {
for(X=0; X<=(originalImage.cols-1); X++) {
sumX = 0;
sumY = 0;

/* image boundaries */
if(Y==0 || Y==originalImage.rows-1)
SUM = 0;
else if(X==0 || X==originalImage.cols-1)
SUM = 0;

/* Convolution starts here */
else {

/*-------X GRADIENT APPROXIMATION------*/
for(I=-1; I<=1; I++) {
for(J=-1; J<=1; J++) {
sumX = sumX + (int)( (*(originalImage.data + X + I + (Y + J)*originalImage.cols)) * GX[I+1][J+1]);
}
}
if(sumX>255) sumX=255;
if(sumX<0) sumX=0;

/*-------Y GRADIENT APPROXIMATION-------*/
for(I=-1; I<=1; I++) {
for(J=-1; J<=1; J++) {
sumY = sumY + (int)( (*(originalImage.data + X + I + (Y + J)*originalImage.cols)) * GY[I+1][J+1]);
}
}
if(sumY>255) sumY=255;
if(sumY<0) sumY=0;

SUM = abs(sumX) + abs(sumY); /*---GRADIENT MAGNITUDE APPROXIMATION (Myler p.218)----*/
}

*(edgeImage.data + X + Y*originalImage.cols) = 255 - (unsigned char)(SUM); /* make edges black and background white */
fwrite( (edgeImage.data + X + Y*originalImage.cols), sizeof(char), 1, bmpOutput);
}
}

printf("See edgeSob.bmp for results\n");
fclose(bmpInput);
fclose(bmpOutput);
farfree(edgeImage.data); /* Finished with edgeImage.data */
farfree(originalImage.data); /* Finished with originalImage.data */
return 0;
}

/*----------GET IMAGE INFO SUBPROGRAM--------------*/
long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char *ptrC;
long value = 0L;
unsigned char mmy;
int i;

mmy = '0';
ptrC = &mmy;

fseek(inputFile, offset, SEEK_SET);

for(i=1; i<=numberOfChars; i++)
{
fread(ptrC, sizeof(char), 1, inputFile);
/* calculate value based on adding bytes */
value = (long)(value + (*ptrC)*(pow(256, (i-1))));
}
return(value);

} /* end of getImageInfo */

/*-------------COPIES HEADER AND INFO HEADER----------------*/
void copyImageInfo(FILE* inputFile, FILE* outputFile)
{
unsigned char *ptrC;
unsigned char mmy;
int i;

mmy = '0';
ptrC = &mmy;

fseek(inputFile, 0L, SEEK_SET);
fseek(outputFile, 0L, SEEK_SET);

for(i=0; i<=50; i++)
{
fread(ptrC, sizeof(char), 1, inputFile);
fwrite(ptrC, sizeof(char), 1, outputFile);
}

}

/*----------------COPIES COLOR TABLE-----------------------------*/
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
{
unsigned char *ptrC;
unsigned char mmy;
int i;

mmy = '0';
ptrC = &mmy;

fseek(inputFile, 54L, SEEK_SET);
fseek(outputFile, 54L, SEEK_SET);

for(i=0; i<=(4*nColors); i++) /* there are (4*nColors) bytesin color table */
{
fread(ptrC, sizeof(char), 1, inputFile);
fwrite(ptrC, sizeof(char), 1, outputFile);
}

}

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

华为手机进了一点水,我尝试充电,然后手机发烫,还有救吗?该怎么办? 万州有无接触蔬菜配送电话? 谁知道万州蔬菜批发市场在哪里啊 关于图像处理,利用sobel算子边缘检测的Matlab程序 万州明海卤菜 重庆万州有蔬菜和粮油批发基地吗,都分别在哪里啊? 万州大碗菜怎么样 4方向或者8方向的sobel算子,怎么求边缘方向 万州的蔬菜批发市场在哪里 万州的特色菜是什么 万州凉拌素菜? 重庆万州特色卤菜 看完《南巫》后完全处于迷糊状态,其中的隐喻属于导演个人的自我陶醉吗? 有没有人遇到什么灵异的事情,真实可信吗 近两年盗墓题材电影很火,盗墓电影吸引你的原因是什么? 世界上真有鬼神吗? 很多年前,无意间做过的梦,在很多年后的今天发生了,一模一样的场景,感觉自己经历过一次一样!猛地想起 为什么有时候现实中发生的事情和梦境中的相似? 中国银行柜台人民币境内汇出汇款附言填写要求 世界上有鬼的存在吗?网上流传的诡异事件都是真实的吗? 手机掉水里修好后发热厉害 用Sobel算子、Roberts算子、Prewitt算子对图像进行边缘检测的程序,在matlab环境下,程序要完整,最好有图 手机进水以后异常发热 臭豆腐包装盒 matlab 关于sobel算子问题 臭豆腐包装配料表写了什么? 手机进水后一直发烫,好像不能散热。电池没有问题。也能正常使用 sobel算子提取边缘,用边缘点统计直方图代码程序 matlab 臭豆腐包装配料表之屎是被恶搞吗? 如何计算边缘检测算子的边缘像素、边缘段以及连续性指标?如下图 手机进水严重发热怎么办? 在C或C++环境下,分别利用sobel算子,robert算子,编程输出边缘图像。选错课了..完全不懂 求高手代码.. 臭豆腐为什么好吃不好闻? VIVOY9S手机进水了但是还能用但是使用之后手机发热怎么办? 臭豆腐的各种风味 刚刚手机进水了 然后插着充电 现在充电口特别烫 有烧焦的味道 怎么办 臭豆腐用真空包装,这样包装起来真的合适吗 在家如何制作传统臭豆腐,味道也能像卖的一样? 关于臭豆腐;请问。。。 我今天买了一个包装袋臭豆腐叫祖名臭豆腐,是不是大便做的,据说现在很多臭豆腐都是臭水沟或者大便做的,