at()函數
函數at()來實現讀去矩陣中的某個像素,或者對某個像素進行賦值操作。下面兩行代碼演示了at()函數的使用方法。
uchar value = grayim.at(i,j);//讀出第i行第j列像素值
grayim.at(i,j)=128; //將第i行第j列像素值設置為128
如果要對圖像進行遍歷,可以參考下面的例程。這個例程創建了兩個圖像,
分別是單通道的grayim以及3個通道的colorim,然后對兩個圖像的所有像素值
進行賦值,最后現實結果。
for( int i = 0; i < colorim.rows; ++i)
for( int j = 0; j < colorim.cols; ++j )
{
Vec3b pixel;
pixel[0] = i%255; //Blue
pixel[1] = j%255; //Green
pixel[2] = 0; //Red
colorim.at(i,j) = pixel;
}
}
改變圖像亮度和透明度的基本實現方法:
實現效果的核心代碼
for( int i = 0; i < imageDpi.rows; i++) {? ? ? ??
? ? ? ? ?for( int j = 0; j < imageDpi.cols; j++) {? ? ? ? ? ?
? ? ? ? ? ? ? ? ?for( int k = 0; k < 3; k++) {? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ?new_image.at(i, j)[k] = saturate_cast(alpha * ( imageDpi.at(i, j)[k]) + betal);
? ? ? ? ?} ? ? ??
? ? ?}
}
為了訪問圖像的每一個像素,使用語法: image.at(i, j)[k] ,其中, i 是像素所在的行, j 是像素所在的列, k 是R、G、B(0、1、2)之一。
用 saturate_cast 對結果進行轉換,以確保它為有效值。
實現效果
實現代碼
// 需要的公共屬性
using namespace cv;
Mat imageDpi;
Mat new_image;
@interface ViewController ()
{
UIImageView *imagView2;
float alpha;
float betal;
}
初始化界面和Mat對象等
UIImageView *imagView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 200)];
[self.view addSubview:imagView1];
imagView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 350, self.view.bounds.size.width, 200)];
[self.view addSubview:imagView2];
UIImage *image = [UIImage imageNamed:@"try.png"];
imagView1.image = image;
// 將圖片轉換為Mat對象
UIImage *image3 = [UIImage imageNamed:@"try.png"];
//? ? Mat imageDpi;
UIImageToMat(image3, imageDpi);
// 初始化接收后最后修改結果的對象
new_image = Mat::zeros(imageDpi.size(), imageDpi.type());
imagView2.image = image;
UISlider *slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0,600, self.view.bounds.size.width, 20)];
slider1.value = 0;
slider1.tag = 1000;
slider1.maximumValue = 3;
[slider1 addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];// 針對值變化添加響應方法
[self.view addSubview:slider1];
UISlider *slider2 = [[UISlider alloc] initWithFrame:CGRectMake(0,630, self.view.bounds.size.width, 20)];
slider2.value = 0;
slider2.tag = 2000;
slider2.maximumValue = 100;
[slider2 addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];// 針對值變化添加響應方法
[self.view addSubview:slider2];
實現方法
- (void)sliderValueChanged:(UISlider *)sender{? ?
?NSLog(@"%f",sender.value);? ?
?if (sender.tag == 1000) {? ? ??
? ? ? ? ? ? alpha = sender.value;? ?
? ? }else{? ? ? ?
? ? ?betal = sender.value;;? ?
? ? ?}? ?
?for( int i = 0; i < imageDpi.rows; i++) {? ? ? ?
for( int j = 0; j < imageDpi.cols; j++) {? ? ? ? ? ?
for( int k = 0; k < 3; k++) {? ? ? ? ? ? ? ?
?new_image.at(i, j)[k] = saturate_cast(alpha * ( imageDpi.at(i, j)[k]) + betal);
}
}
}
// 最后需要將 Mat 轉換為 UIImage 對象
UIImage *image = MatToUIImage(new_image);
imagView2.image = image;
}