如何使用matlab实现统计图像中灰度级的数目

如何使用matlab实现统计图像中灰度级的数目,第1张

可以配合使用函数find和函数length或者函数find和函数length求得。

1、函数find和函数length配隐庆隐合使用获得像差链素为value的像素个数

1

2

3

4

5

6

7

8

% 读入数据112.bmp是要读入的图像

I = imread('112.bmp')

% 如果为彩色图像需转为灰度图像,否则不需要

I = rgb2gray(I)

% value为要计算的像素值

[r,c]=find(I==value)

% 获得个数

num=length(r)

2、函数find和函数sum配合使用获得像素为value的像素个数

1

2

3

4

5

6

% 读入数据112.bmp是要读入的图像

I = imread('112.bmp')

% 如果为彩色图像需转为灰度图像,否则不需要

I = rgb2gray(I)

% 获得像素值为value的素值个数灶厅

num=sum(sum(I==value))

JSP页面中嵌入动态图表的两种方法 :在JSP页面中插入Applet小程序 通过JavaBean动态生成图像。

JSP是一种广泛应用的网页设计技术 ,它是一种HTML和Java脚本混合的编程技术 ,它结合了HTML的静态特性和Java语言的动态能力 ,因此做碧友用它进行动态网页设计非常方便。在进行图像处理时 ,一般处理静态图纯槐片非常容易 ,但是 ,在实际应用中常常需要动态地在网页中生成二维的图形.

基于JFreeChart开发的一个时序图的绘制。代码如下:

实例中createDataset()方法用于创建数据集合对象。时序图慧吵的数据集合与其他数据集合不同,它需要添加一个时间段内的所有数据,通常采用TimeSeries类进行添加。该实例中通过Math类的random()方法进行随机生成。

import java.awt.*import java.awt.event.ActionEventimport java.awt.event.ActionListenerimport java.io.BufferedInputStreamimport java.io.DataInputStreamimport java.io.FileOutputStreamimport java.io.IOExceptionimport java.net.URLimport java.net.URLConnectionimport java.text.DateFormatimport java.text.ParseExceptionimport java.text.SimpleDateFormatimport java.util.Calendarimport java.util.Dateimport java.util.Random

import javax.swing.JAppletimport javax.swing.Timer

import org.jfree.chart.*import org.jfree.chart.annotations.CategoryTextAnnotationimport org.jfree.chart.axis.CategoryAnchorimport org.jfree.chart.axis.CategoryAxisimport org.jfree.chart.axis.CategoryLabelPositionsimport org.jfree.chart.axis.DateAxisimport org.jfree.chart.axis.DateTickUnitimport org.jfree.chart.axis.DateTickUnitTypeimport org.jfree.chart.axis.ValueAxisimport org.jfree.chart.labels.StandardCategoryItemLabelGeneratorimport org.jfree.chart.plot.CategoryPlotimport org.jfree.chart.plot.PlotOrientationimport org.jfree.chart.plot.XYPlotimport org.jfree.chart.renderer.category.BarRendererimport org.jfree.chart.title.TextTitleimport org.jfree.data.category.CategoryDatasetimport org.jfree.data.category.IntervalCategoryDataset

import org.jfree.chart.axis.NumberAxisimport org.jfree.data.category.DefaultCategoryDatasetimport org.jfree.data.gantt.Taskimport org.jfree.data.gantt.TaskSeriesimport org.jfree.data.gantt.TaskSeriesCollectionimport org.jfree.data.time.Dayimport org.jfree.data.time.Secondimport org.jfree.data.time.TimeSeriesimport org.jfree.data.time.TimeSeriesCollectionimport org.jfree.data.xy.XYDataset

public class shixutu extends JApplet {

//PLOT_FONT是一静态的字体常量对象,使用此对象可以避免反复用到的字体对象被多次创建        private static final Font PLOT_FONT = new Font("黑体", Font.ITALIC , 18)        JFreeChart chart

//创建数据动态更新的监听  class DataGenerator extends Timer implements ActionListener {         private static final long serialVersionUID = 3977867288743720504L         String equID                                 //设备ID号         int totalTask                                //任务数         String[][] strTask                           //任务情况

public void actionPerformed(ActionEvent actionevent) {             addTotalObservation()         }         DataGenerator() {                       super(1000, null)             addActionListener(this)             System.out.println("super")         }     }       //将更新的数据添加到chart中     private void addTotalObservation() {       System.out.println("addTotalObservation")          //设置新的数据集            chart.getXYPlot().setDataset(createDataset())          //通知Jfreechart 数据发生了改变,重新绘制柱状图          if (chart != null) {              chart.fireChartChanged()          }      }        private static void processChart(JFreeChart chart) {                   //设置标题字体                   chart.getTitle().setFont(new Font("隶书", Font.BOLD, 26))                   //设置背景色                   chart.setBackgroundPaint(new Color(252,175,134))                   XYPlot plot = chart.getXYPlot()        //获取图表的绘制属性                   plot.setDomainGridlinesVisible(false)  //设置网格不显示                   //获取时间轴对象                   DateAxis dateAxis = (DateAxis) plot.getDomainAxis()                   dateAxis.setLabelFont(PLOT_FONT)   //设置时间轴字体                   //设置时间轴标尺值字体                   dateAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,12))                   dateAxis.setLowerMargin(0.0)       //设置时间轴上显示的最小值                   //获取数据轴对象                   ValueAxis valueAxis = plot.getRangeAxis()                   valueAxis.setLabelFont(PLOT_FONT)                      //设置数据字体                   DateFormat format = new SimpleDateFormat("mm分ss秒")   //创建日期格式对象                   //创建DateTickUnit对象                   DateTickUnit dtu = new DateTickUnit(DateTickUnitType.SECOND,30,format)                   dateAxis.setTickUnit(dtu)          //设置日期轴的日期标签           }          //将结果输出在文件中

private static void writeChartAsImage(JFreeChart chart) {                FileOutputStream fos_jpg = null                try {                    fos_jpg = new FileOutputStream("D:\\test\\shixutu.jpg")                    ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null)                } catch (Exception e) {                   e.printStackTrace()                } finally {                    try {                        fos_jpg.close()                   } catch (Exception e) {                    }                }            }            //创建数据集合对象           public static XYDataset createDataset() {                     //实例化TimeSeries对象                      TimeSeries timeseries = new TimeSeries("Data")                      Second second = new Second()  //实例化Day                                        double d = 50D                      //添加一年365天的数据                      for (int i = 0i <500i++) {                           d = d + (Math.random() - 0.5) * 10 //创建随机数据                          timeseries.second(day, d) //向数据集合中添加数据                          second = (Second) second.next()                      }                   TimeSeriesCollection timeSeriesCollection =                          new TimeSeriesCollection(timeseries)                    //返回数据集合对象                    return timeSeriesCollection           } //Applet程序初始化

public void init() {        // 1. 得到数据        XYDataset  dataset = createDataset()              // 2. 构造chart               chart = ChartFactory.createTimeSeriesChart(                     "时序图示范", // 图表标题                      "时间", // 目录轴的显示标签--横轴                      "数值", // 数值轴的显示标签--纵轴                      dataset, // 数据集                      false,                    false, // 是否生成工具                      false // 是否生成URL链接                      )             // 3. 处理chart中文显示问题              processChart(chart)                 // 4. chart输出图片              //writeChartAsImage(chart)            // 5. chart 以swing形式输出               //6.使用applet输出            ChartPanel chartPanel = new ChartPanel(chart)            chartPanel.setPreferredSize(new java.awt.Dimension(800,500))                   getContentPane().add(chartPanel)          (new DataGenerator()).start()         }         public void paint(Graphics g) {             if (chart != null) {                chart.draw((Graphics2D) g, getBounds())             }         }    public void destroy() {    }}

你的意思是最大灰度值吧?如果是的话程序如下:

#include "stdafx.h"

#include <iostream>

#include <cv.h>

#include <cxcore.h>

#include <highgui.h>

using namespace std

void main()

{

IplImage* src_img=cvLoadImage("0.bmp") //导入图片

size_t min_value=0 //图像最小灰度值

size_t max_value=0 //世吵图像最小灰度值

size_t intensity = 0 //每个像素灰度值

size_t height=src_img->height

size_t width =src_img->width

for (size_t rows=0rows<heightrows++)

{

uchar* ptr = (uchar*)src_img->imageData+rows*width

for (size_t cols=0cols<widthcols++)

{

intensity=(int)ptr[cols]

// cout<<intensity<<"\t"

min_value=(min_value<intensity)?min_value:intensity

max_value=(max_value<intensity)?intensity:max_value

}

}

cout<<"图像最小灰度值为:"简返升<<min_value<<endl

<<"图像最大灰度值为:拦老"<<max_value<<endl

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12389988.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存