iT邦幫忙

0

有關用C#的數位影像處理

  • 分享至 

  • xImage

題目:
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)
請各路好手幫幫忙

看更多先前的討論...收起先前的討論...
sion iT邦新手 4 級 ‧ 2019-10-14 15:48:23 檢舉
所以你的問題是啥???
k28725727 iT邦新手 5 級 ‧ 2019-10-14 16:05:49 檢舉
就是要把程式改成題目所要的
fillano iT邦超人 1 級 ‧ 2019-10-14 17:01:35 檢舉
題目要的是什麼?
ccutmis iT邦高手 2 級 ‧ 2019-10-14 17:12:06 檢舉
reducing the number of intensity levels in a image from 256 to 4
是指把一張圖片的色階從256色降到4色嗎?
k28725727 iT邦新手 5 級 ‧ 2019-10-14 17:17:51 檢舉
用"2的n次方來處理灰階強度,從256降到4,
fillano iT邦超人 1 級 ‧ 2019-10-14 17:34:09 檢舉
但是好像又要能把調降後的色階當作變數。

另外,這個程式幾乎每行都有註解?看起來有點怪XD

threshold的做法僅適用於黑白的case,其他可能就是用除法然後調到近似的整數值。要快的話,可能直接把數值右移n位,其中前後色階相除是二的n次方。
ccutmis iT邦高手 2 級 ‧ 2019-10-14 19:07:33 檢舉
>這個程式幾乎每行都有註解
通常不是寫給別人看就是....抄來的^^"
小魚 iT邦大師 1 級 ‧ 2019-10-14 19:21:42 檢舉
看起來應該是抄來的,
請先研究一下程式碼,
如果看不懂的話,
請先從基礎開始學起.
小魚 iT邦大師 1 級 ‧ 2019-10-14 19:22:48 檢舉
如果是台灣人寫的,
至少註解會用中文寫吧,
這個程式連Google翻譯一下都省了耶...
dragonH iT邦超人 5 級 ‧ 2019-10-14 22:59:35 檢舉
>如果是台灣人寫的,
至少註解會用中文寫吧,

不一定喔

我遇過蠻多都是用英文註解

用中文反而占少數XD
小魚 iT邦大師 1 級 ‧ 2019-10-15 10:34:27 檢舉
好吧,
我英文不好,
都是用中文註解的.
dragonH iT邦超人 5 級 ‧ 2019-10-15 11:09:32 檢舉
習慣問題啦XD
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
小魚
iT邦大師 1 級 ‧ 2019-10-14 17:05:53

你可以發去StackOverflow,
我們這裡是中文論壇.

米歐 iT邦新手 3 級 ‧ 2019-10-15 09:42:31 檢舉

/images/emoticon/emoticon01.gif

我要發表回答

立即登入回答