感知哈希算法 bcb实现 有些问题 希望大牛能知道知道
头文件
//---------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Dialogs.hpp>
#include <ExtDlgs.hpp>
#include <Buttons.hpp>
struct rgb_str
{
unsigned char r_color;
unsigned char g_color;
unsigned char b_color;
};
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TButton *Button1;
TImage *Image1;
TOpenPictureDialog *OpenPictureDialog1;
TBitBtn *BitBtn1;
TImage *Image2;
TButton *Button2;
TImage *Image3;
void __fastcall Button1Click(TObject *Sender);
void __fastcall BitBtn1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private:// User declarations
public:// User declarations
__fastcall TForm1(TComponent* Owner);
Byte* ReduceColor(TImage *image);
AnsiString PictureFileName;
Graphics::TBitmap *bitmap;
rgb_str rgb[2000][2000];
Byte CalcAverage(Byte* values );
void ReduceColor4(TImage *image);
int len;
String ComputeBits(Byte * values, Byte averageValue);
int TForm1::CalcSimilarDegree(String a, String b);
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
cpp文件
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <jpeg.hpp>
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Graphics::TBitmap *MyBMP = new Graphics::TBitmap;
Graphics::TBitmap *Dest = new Graphics::TBitmap;
Graphics::TBitmap *Dest2 = new Graphics::TBitmap;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenPictureDialog1->Execute())
{
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
PictureFileName = OpenPictureDialog1->FileName;
}
//缩放成8*8像素大小
MyBMP->Assign(Image1->Picture->Bitmap);
Dest->Width=8;
Dest->Height=8;//张,宽你最好计算清楚,满足你的缩放要求
SetStretchBltMode(Dest->Canvas->Handle,COLORONCOLOR);//增加图形的品质
Dest->Canvas->StretchDraw(TRect(0,0,8,8),MyBMP);//具体在这里控制放大缩小
//也可以改用其他方法,如copyRect
Dest->SaveToFile("d:\dest.bmp") ;
Dest2->Assign(Dest);
Image2->Picture->Assign(Dest2);
}
//---------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Image1->Picture->Assign(NULL);
Image2->Picture->Assign(NULL);
// delete MyBMP;
// delete Dest;
// delete Dest2;
}
//---------------------------------------
Byte* TForm1::ReduceColor(TImage *image)
{
len=0;
// Step 2 : Reduce Color 将缩小后的图片, 转为64级灰度图片.
TColor color;
Byte* grayValues=new Byte[image->Width*image->Height];
for(int x = 0; x<image->Width ; x++)
{
for ( int y = 0; y < image->Height ; y++)
{
color=image->Canvas->Pixels[x][y];
rgb[x][y].r_color=GetRValue(color);
rgb[x][y].g_color=GetGValue(color);
rgb[x][y].b_color=GetBValue(color);
Byte grayValue = (Byte)((rgb[x][y].r_color * 0.3 + rgb[x][y].g_color * 0.59 + rgb[x][y].b_color* 0.11) / 100);
image->Canvas->Pixels[x][y]=(TColor)grayValue;
grayValues[x* image->Width + y] = grayValue;
//len ++;
}
}
image->Picture->SaveToFile("d:\sddd.bmp");
return grayValues;
}
// Step 3 : Average the colors 计算灰度平均值
Byte TForm1::CalcAverage (Byte* values )
{
int sum = 0;
for (int i = 0; i <64; i++)
sum += (int)values[i];
return (Byte)(sum /64);
}
// Step 4 : Compute the bits 第四步 比较像素的灰度
String ComputeBits(Byte* values, Byte averageValue)
{
char result[64];
for (int i = 0; i < 64; i++)
{
if (values[i] < averageValue)
result[i] = '0';
else
result[i] = '1';
}
return (String)result;
}
int TForm1::CalcSimilarDegree(String a, String b)
{
if (a.Length() != b.Length())
ShowMessage("有异常出现");
int count = 0;
for (int i = 0; i < a.Length(); i++)
{
if (a[i] != b[i])
count++;
}
return count;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Byte* grayV = ReduceColor(Image2);
Byte avg= CalcAverage(grayV);
String rs=ComputeBits(grayV,avg);
}
//---------------------------------------
void TForm1::ReduceColor4(TImage *img)
{
unsigned int B,G,R,PixelValue;
for(int row=0;row<img->Width;row++)
for(int column=0;column<img->Height;column++)
{
PixelValue=img->Canvas->Pixels[row][column];
B=(PixelValue&0x00ff0000)>>16;
G=(PixelValue&0x0000ff00)>>8;
R=(PixelValue&0x000000ff);
R = G = B = 0.299*R+0.587*G+0.114*B;
img->Canvas->Pixels[row][column]=TColor(RGB(R,G,B));
}
img->Picture->SaveToFile("d:\ff.bmp");
}
出现一下错误
[Linker Error] Unresolved external 'TForm1::ComputeBits(unsigned char *, unsigned char)' referenced from D:\LEARN\TUXIANGSOUSUO\UNIT1.OBJ
小弟是新手 ,不知道各位有没有其他的好方法,有的话请告诉小弟,让小弟颈部一些 歇歇 算法 rgb String
[解决办法]
很明显:
String ComputeBits(Byte* values, Byte averageValue)
改为:
String TForm1::ComputeBits(Byte* values, Byte averageValue)