題目:
Reducing the Number of Intensity Levels in an Image:
Write a computer program capable of reducing the number of intensity levels in a image from 256 to 4, in integer powers of 2. The desired number of intensity levels needs to be a variable input to your program.
但我寫出來的程式,沒有達到題目的要求
我的程式碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MiM_iVision; // using namespace of MiM Image Library that can use their Function
namespace
{
public partial class Form1 : Form
{
public IntPtr GrayImage = iImage.CreateGrayiImage();
public IntPtr hbitmap;
E_iVision_ERRORS err = E_iVision_ERRORS.E_NULL;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
iImage.DestroyiImage(GrayImage); // Release the image struction before shut down the Application
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "BMP file |* .bmp";
string filepath; // Declare a string type of variable
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filepath = openFileDialog1.FileName;
err = iImage.iReadImage(GrayImage, filepath);
if (err == E_iVision_ERRORS.E_OK)
{
hbitmap = iImage.iGetBitmapAddress(GrayImage); //Get GrayImage's hbitmap
if (pictureBox1.Image != null) //If there is an image on the Picturebox
pictureBox1.Image.Dispose(); //clear Picturebox image
pictureBox1.Image = System.Drawing.Image.FromHbitmap(hbitmap);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //Set PictureBox size mode
pictureBox1.Refresh(); // refresh to update the Picturebox
}
else
MessageBox.Show(err.ToString(), "Error");
}
}
private void button2_Click(object sender, EventArgs e)
{
int Width = iImage.GetWidth(GrayImage); // Get image width
int Height = iImage.GetHeight(GrayImage); // Get image height
int Threshold = Convert.ToInt32(tb_Threshold.Text);
IntPtr GrayImage1 = iImage.CreateGrayiImage(); ;
iImage.iImageResize(GrayImage1, Width, Height);
iImage.iImageCopy(GrayImage1, GrayImage);
byte[,] Graymatrix = new byte[Height, Width];
err = iImage.iPointerFromiImage(GrayImage1, ref Graymatrix[0, 0], Width, Height);
if (err != E_iVision_ERRORS.E_OK) // Check the status from functions
{
MessageBox.Show(err.ToString(), "ERROR"); // This will open a MessagBox for warning.
return; // End "Binary Threshold Event Function"
}
// start sliding the matrix
for (int i = 0; i < Height; i++) // i index for cols ( 0~Hight-1)(Because that matrix index is start from 0)
{
for (int j = 0; j < Width; j++) // j index for rows ( 0~Width-1)
{
if (Graymatrix[i, j] < Threshold) // Compare if the image intensity of each pixel smaller than threshold
Graymatrix[i, j] = 0; // Set pixel to 0 (Black)
else
Graymatrix[i, j] = 255; // Set pixel to 1 (White)
}
}
IntPtr imgPtr = iImage.iVarPtr(ref Graymatrix[0, 0]);
err = iImage.iPointerToiImage(GrayImage1, imgPtr, Width, Height);
if (err != E_iVision_ERRORS.E_OK) // Check the status from functions
{
MessageBox.Show(err.ToString(), "Error");
return;
}
hbitmap = iImage.iGetBitmapAddress(GrayImage1); // transform to hbitmap for PictureBox
if (pictureBox1.Image != null) //If there is an image on the Picturebox
pictureBox1.Image.Dispose(); //clear Picturebox image
pictureBox1.Image = System.Drawing.Image.FromHbitmap(hbitmap); // shows image
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //Set PictureBox size mode
pictureBox1.Refresh(); // refresh to update the Picturebox
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
好像下面要改,但我不知道要怎麼改,我在這邊卡關了
for (int j = 0; j < Width; j++) // j index for rows ( 0~Width-1)
{
if (Graymatrix[i, j] < Threshold) // Compare if the image intensity of each pixel smaller than threshold
Graymatrix[i, j] = 0; // Set pixel to 0 (Black)
else
Graymatrix[i, j] = 255; // Set pixel to 1 (White)
請各路好手幫幫忙