用C进行图像操作 [英] Image operation in C

查看:134
本文介绍了用C进行图像操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像处理添加到我的应用中,并且还想计算完成渲染图像所需的时间.我还需要一些有关如何存储图像的想法.

I want to add image processing to my app and also want to calculate the time it takes to finish rendering an image. I also need some ideas on how I can store images.

推荐答案

有大量免费的图像处理库,其中许多是开源的.

我的前2名是:
CxImage [ GDI + [
There are a vast number of image processing libraries that are free and a lot of them are open source.

My top 2 would be:
CxImage[^] - Very comprehensive
GDI+[^] - pretty easy to use, well documented and heaps of samples

As for the timing, that is simple. You just need to use a timing function like timeGetTime or if you want some serious precision QueryPerformanceCounter

#include <StdIO.h>
#include <Windows.h>
#include <MmSystem.h>

#pragma comment(lib, "WinMm.lib") //link with WinMM.dll for timeGetTime()

int main() {
    //timeGetTime() returns an arbritary time, so we need to get the starting time
    DWORD nStart = timeGetTime();
    Sleep(2000); //Process your images here
    DWORD nEndTime = timeGetTime();
    printf("Operation took %ums\n", nEndTime - nStart);
    return 0;
}



此代码正常运行在我的Ubuntu虚拟机上,而不在Windows上,因为Windows仅提供毫秒级的精度,除非您使用性能计数器.
因此,Windows将显示时间为3000、2000、1000或偶尔为0微秒.

如果您考虑在某个阶段移植到Windows,请记住这一点.

This code works properly on my Ubuntu virtual machine, but not on Windows as Windows only provides precision to the millisecond unless you use the performance counters.
Windows will show the time as either 3000, 2000, 1000 or occasionally 0 microseconds because of this.

Keep this in mind if you are considering on porting to Windows at some stage.

#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>

unsigned int GetTimeMilli() {
	struct timeval sTime;
	struct timezone sTimeZone;
	struct tm *tmNow;
	unsigned int nTime;
	gettimeofday(&sTime, &sTimeZone);
	tmNow = localtime(&sTime.tv_sec);
	nTime = tmNow->tm_hour;
	nTime = nTime * 60 + tmNow->tm_min;
	nTime = nTime * 60 + tmNow->tm_sec;
	nTime = nTime * 1000 + sTime.tv_usec / 1000; //convert micro to milli
	return nTime;
}

unsigned long long GetTimeMicro() {
	struct timeval sTime;
	struct timezone sTimeZone;
	struct tm *tmNow;
	unsigned long long nTime;
	gettimeofday(&sTime, &sTimeZone);
	tmNow = localtime(&sTime.tv_sec);
	nTime = tmNow->tm_hour;
	nTime = nTime * 60 + tmNow->tm_min;
	nTime = nTime * 60 + tmNow->tm_sec;
	nTime = nTime * 1000000 + sTime.tv_usec; //convert micro to milli
	return nTime;
}

unsigned int GetTimeDiffMilli(unsigned int nStart, unsigned int nEnd) {
	if (nEnd < nStart) { //we ended in a different day than we started in
		return nEnd + (24 * 60 * 60 * 1000000) - nStart;
	}
	return nEnd - nStart;
}

unsigned long long GetTimeDiffMicro(unsigned long long nStart, unsigned long long nEnd) {
	if (nEnd < nStart) { //we ended in a different day than we started in
		return nEnd + (24 * 60 * 60 * 1000000) - nStart;
	}
	return nEnd - nStart;
}

int main() {
	unsigned long long nStart, nEnd;
	nStart = GetTimeMicro();
	usleep(2000000); //Sleep for 2,000,000 microseconds = 2 seconds
	nEnd = GetTimeMicro();
	printf("%llu, %llu\n", nStart, nEnd);
	printf("Sleep took %llu microseconds\n", GetTimeDiffMicro(nStart, nEnd));
	return 0;
}


这篇关于用C进行图像操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆