C#實(shí)戰(zhàn):圖像清晰度增強(qiáng)介紹和案例實(shí)踐
一、圖像清晰度增強(qiáng)介紹

基于騰訊云深度學(xué)習(xí)等人工智能技術(shù),消除圖片因有損壓縮導(dǎo)致的噪聲,改善因使用濾鏡、拍攝失焦等導(dǎo)致的圖像模糊問題,讓圖片的邊緣和細(xì)節(jié)更加清晰自然。
二、開發(fā)流程
首先登錄騰訊云平臺開通圖片處理服務(wù) 官網(wǎng):https://cloud.tencent.com/product/imageprocess
申請開發(fā)密鑰
下載編程語言對應(yīng)的SDK
開發(fā)工具創(chuàng)建項(xiàng)目
引用騰訊圖像處理庫
根據(jù)業(yè)務(wù)編寫代碼
三、請求參數(shù)說明
● Region:資源地域,必填,表示操作的資源所屬的地域,比如 ap-shanghai ap-beijing ap-shenzhen 等。

● ImageUrl:圖片URL地址參數(shù)。圖片格式:PNG、JPG、JPEG。 圖片大?。核螺d圖片經(jīng)Base64編碼后不超過4M。圖片下載時(shí)間不超過3秒。
● ImageBase64:支持PNG、JPG、JPEG、BMP,不支持 GIF 圖片。圖片經(jīng)過Base64編碼的內(nèi)容。最大不超過4M。與ImageUrl同時(shí)存在時(shí)優(yōu)先使用ImageUrl字段。注意:圖片需要Base64編碼,并且要去掉編碼頭部。
四、輸出參數(shù)說明
● EnhancedImage:增強(qiáng)后圖片的base64編碼。
示例值:/9j/4AAQSkZJRgABAQAAAQABA…
● RequestId:唯一請求 ID,每次請求都會返回。排查異常問題時(shí)需要提供該次請求的 RequestId。
五、開發(fā)實(shí)踐
這里采用SDK+C#語言的方式寫一個WinForm程序。具體操作如下:
首先新建一個WinForm控制臺程序EnhanceImageDemo,框架選擇NetFramework4.5.2。

1.安裝依賴庫TencentCloudSDK.Tiia
通過命令行:
dotnet add package TencentCloudSDK.Tiia通過Nuget 包管理器方式安裝。
打開nuget包管理器,搜索 TencentCloudSDK.Tiia,安裝最新穩(wěn)定版本 3.0.957。

2.增加騰訊API調(diào)用配置項(xiàng)
直接在app.config 文件添加,完整的內(nèi)容如下:
<?xml versinotallow="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime versinotallow="v4.0" sku=".NETFramework,Versinotallow=v4.5.2" />
    </startup>
		<appSettings>
			<!--SecretId-->
			<add key="SecretId" value="xxxxxx"/>
			<!--SecretKey-->
			<add key="SecretKey" value="xxxxxx"/>
			<!--地區(qū)選擇ap-shanghai ap-beijing ap-shenzhen 等-->
			<add key="Region" value="ap-shanghai"/>
			<!--圖片輸出目錄-->
			<add key="OutPath" value="D:\Image\"/>
</appSettings>
</configuration>注意:開發(fā)密鑰需要開發(fā)者自己申請?zhí)钊肱渲梦募?/span>

3.代碼
主要實(shí)現(xiàn)了網(wǎng)絡(luò)圖片URL調(diào)用圖像增強(qiáng)處理方法生成Base64字符串,然后轉(zhuǎn)換為png圖片,并直接調(diào)用操作系統(tǒng)默認(rèn)的打開圖片工具直接打開圖片。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Tiia.V20190529;
using TencentCloud.Tiia.V20190529.Models;
using System.Configuration;
namespace EnhanceImageDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 圖片增強(qiáng)方法
        /// </summary>
        private void EnhanceImage()
        {
            string url = textBox1.Text.Trim();
            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                MessageBox.Show("請輸入正確的圖片URL");
            }
            else
            {
                // 調(diào)用騰訊云接口的參數(shù)說明
                string secretId = ConfigurationManager.AppSettings["SecretId"];
                string secretKey = ConfigurationManager.AppSettings["SecretKey"];
                string region = ConfigurationManager.AppSettings["Region"];
                try
                {
                    // 實(shí)例化一個認(rèn)證對象,入?yún)⑿枰獋魅腧v訊云賬戶 SecretId 和 SecretKey,需注意密鑰對的保密             
                    // 密鑰可前往官網(wǎng)控制臺 https://console.cloud.tencent.com/cam/capi 進(jìn)行獲取
                    Credential cred = new Credential
                    {
                        SecretId = secretId,
                        SecretKey = secretKey
                    };
                    // 實(shí)例化一個client選項(xiàng),可選的,沒有特殊需求可以跳過
                    ClientProfile clientProfile = new ClientProfile();
                    // 實(shí)例化一個http選項(xiàng),可選的,沒有特殊需求可以跳過
                    HttpProfile httpProfile = new HttpProfile();
                    httpProfile.Endpoint = ("tiia.tencentcloudapi.com");
                    clientProfile.HttpProfile = httpProfile;
                    // 實(shí)例化要請求產(chǎn)品的client對象,clientProfile是可選的
                    TiiaClient client = new TiiaClient(cred, region, clientProfile);
                    // 實(shí)例化一個請求對象,每個接口都會對應(yīng)一個request對象 傳遞參數(shù),支持網(wǎng)絡(luò)圖片和圖片經(jīng)過Base64編碼的內(nèi)容
                    EnhanceImageRequest req = new EnhanceImageRequest();
                    //圖片URL地址參數(shù)。圖片格式:PNG、JPG、JPEG。 圖片大?。核螺d圖片經(jīng)Base64編碼后不超過4M。圖片下載時(shí)間不超過3秒。
                    req.ImageUrl = url;
                    //ImageBase64 參數(shù) 支持PNG、JPG、JPEG、BMP,不支持 GIF 圖片。圖片經(jīng)過Base64編碼的內(nèi)容。最大不超過4M。與ImageUrl同時(shí)存在時(shí)優(yōu)先使用ImageUrl字段。注意:圖片需要Base64編碼,并且要去掉編碼頭部。
                    req.ImageBase64 = "無";
                    // 返回的resp是一個EnhanceImageResponse的實(shí)例,與請求對象對應(yīng)
                    EnhanceImageResponse resp = client.EnhanceImageSync(req);
                     convertToImage(resp.EnhancedImage);             
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
        }
        /// <summary>
        /// base64字符串轉(zhuǎn)換為png
        /// </summary>
        /// <param name="base64String"></param>
        private void convertToImage(string base64String)        
        {
            // 把Base64 字符串解碼為字節(jié)數(shù)組
            byte[] imageBytes = Convert.FromBase64String(base64String);
            // 配置文件圖片圖片輸出目錄
            string path = ConfigurationManager.AppSettings["OutPath"];
            string fileName = path + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
            // 將字節(jié)數(shù)組保存為圖片文件
            using (MemoryStream ms = new MemoryStream(imageBytes))
            {
                Image image = Image.FromStream(ms);
                // 保存圖片
                image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                // 調(diào)用系統(tǒng)默認(rèn)程序打開圖片文件
                Process.Start(fileName);
            }
        }
        /// <summary>
        /// 按鈕點(diǎn)擊事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            EnhanceImage();
        }
    }
}4.運(yùn)行效果
查看調(diào)用接口成功的調(diào)試數(shù)據(jù)。

頁面運(yùn)行窗體,主要是圖片網(wǎng)絡(luò)地址輸入和轉(zhuǎn)換按鈕。

轉(zhuǎn)換之后成功的效果如下圖:
















 
 
 


 
 
 
 